Compile the Hello World program

Similar documents
CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

Operating Systems, Final exam May 2016 Bachelor's Degree in Computer Science and Engineering

Operating systems fundamentals - B06

CS510 Operating System Foundations. Jonathan Walpole

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

POSIX Semaphores. Operations on semaphores (taken from the Linux man page)

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Concurrent Programming. Concurrent Programming with Processes and Threads

Process Synchronization

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Shared Memory: Virtual Shared Memory, Threads & OpenMP

LSN 13 Linux Concurrency Mechanisms

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS333 Intro to Operating Systems. Jonathan Walpole

Synchronization. Semaphores implementation

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

CS240: Programming in C

CS 105, Spring 2007 Ring Buffer

CSEN 602-Operating Systems, Spring 2018 Practice Assignment 2 Solutions Discussion:

Linux Programming

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Synchronization Primitives

Threads. Threads (continued)

Assignment #2. Problem 2.1: airplane synchronization

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Midterm Exam Questions (document version 1.1)

CS510 Operating System Foundations. Jonathan Walpole

Process management 1

Reading compiler errors

pthreads CS449 Fall 2017

518 Lecture Notes Week 3

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

CSE 380: Homework 2: Synchronization

Gabrielle Evaristo CSE 460. Lab Shared Memory

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Threaded Programming. Lecture 9: Alternatives to OpenMP

CS444 1/28/05. Lab 03

Part II Processes and Threads Process Basics

Interacting with Unix

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS. Information and Computer Science Department. ICS 431 Operating Systems. Lab # 9.

CSC 1600 Unix Processes. Goals of This Lecture

CS 153 Lab6. Kishore Kumar Pusukuri

POSIX Threads. Paolo Burgio

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

CS 3305 Intro to Threads. Lecture 6

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

real time operating systems course

Operating Systems Lab

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*.

POSIX Threads. HUJI Spring 2011

CS 105, Spring 2015 Ring Buffer

Operating Systems, laboratory exercises. List 2.

ECE 650 Systems Programming & Engineering. Spring 2018

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

CS240: Programming in C

Project 4: Synchronization

CS533 Concepts of Operating Systems. Jonathan Walpole

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Final Exam Questions (document version 1.1) WITH SELECTED SOLUTIONS

POSIX PTHREADS PROGRAMMING

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

TCSS 422: OPERATING SYSTEMS

Chapter 4 Concurrent Programming

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

Multi-threaded Programming

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Fall 2015 COMP Operating Systems. Lab #3

Threads. Jo, Heeseung

CSC209 Fall Karen Reid 1

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

PRACE Autumn School Basic Programming Models

Interrupts, Fork, I/O Basics

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

Creating Threads. Programming Details. COMP750 Distributed Systems

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSE 333 SECTION 9. Threads

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Computer Systems Assignment 2: Fork and Threads Package

Dynamic memory allocation

CS 261 Fall Mike Lam, Professor. Threads

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

C Grundlagen - Threads

Project 2: Shell with History1

CSC209F Midterm (L0101) Fall 1998 University of Toronto Department of Computer Science

ECE264 Spring 2014 Exam 2, March 11, 2014

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Transcription:

OS Project1 1

Hello World Writing a Hello World program #include header.h main ( int argc, char *argv[] ) { printf( Hello World!\n ); } Compile the Hello World program > gcc helloworld.c o helloworld 2

Header.h #ifndef HEADER_H_ #define HEADER_H_ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <wait.h> #include <signal.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <ctype.h> #include <sys/time.h> #include <pthread.h> #include <semaphore.h> #endif /*HEADER_H_*/ 3

Sample makefile Name your makefile makefile Sample makefile for the first problem: all: helloworld clear: rm *.o helloworld : helloworld.o gcc helloworld.o -o helloworld helloworld.o: helloworld.c header.h gcc -c helloworld.c 4

makefile Create a line to compile everything all: basic_server basic_client Create a line to remove all object files clear: rm *.o Create a line for each object file you wish to make basic_server.o: basic_server.cc header.h gcc -c basic_server.cc -o basic_server.o Create a line for each executable file you wish to make basic_server: basic_server.o gcc basic_server.o -o basic_server Compile executables: > make 5

Problem 1 Sample MyCopy 1 Open File Stream Object: FILE fopen(char *filename,char *mode) for read and write. r : Read only. w+ : Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed. int fclose(file *stream ) to close file stream object after use. Check errors 6

Problem 1 Sample MyCopy 2 Read and write to a file Read/Write a block of chars: size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); fread returns the number of full items actually read size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); fwrite returns the number of full items actually written Read an individual char: int fgetc( FILE *stream ); int fputc( int c, FILE *stream ); 7

Timing I Problem 1 Sample MyCopy 3 clock_t start, end; double elapsed; start = clock(); end = clock(); elapsed = ((double) (end - start)) / CLOCKS_PER_SEC * 1000; printf("time used: %f millisecond\n", elapsed); 8

Timing II Problem 1 Sample MyCopy 4 struct timeval starttime, endtime; struct timezone tz; struct tm *tm; gettimeofday(&starttime, &tz); gettimeofday(&endtime, &tz); long run_time_in_microseconds; run_time_in_microseconds = endtime.tv_usec - starttime.tv_usec; printf(" Time used: %d microseconds.\n",run_time_in_microseconds); 9

Problem 1 Use fork() Fork the child process: pid_t ForkPID; ForkPID = fork(); Write a quick switch statement: switch (ForkPID) { // -1, fork failure case -1: printf("error: Failed to fork.\n"); break; // 0, this is the child process case 0: break; // > 0, parent process and the PID is the child's PID default: } 10

Problem 1 Using exec() execve(char * filename,char * argv[ ],char * envp[ ]); execlp(char *filename, char *argv0, char *argv1,..., NULL); execvp(char *filename, char *argv [ ]); Run the above functions in a forked child: char* command_array[] = {./MyCopy,argv[1],argv[2],0}; execvp(command_array[0],command_array); The parent thread can wait for the forked thread to complete: while (wait(&status)!= 0) {} Or wait(&status); 11

Problem 2 Tips Part 1 Sample code run in the child process: if (execvp(command_array[0],command_array) == -1) { printf("error: running command: '%s'\n", line); exit(0);} For example: line = ls l command_array[0] = ls command_array[1] = -l command_array[2] = NULL 12

Problem 2 Tips Part 2 Parsing command int parseline(char *line, char *command_array[]) { char *p; int count=0; p = strtok(line, " "); while (p && strcmp(p," ")!=0) { command_array[count] = p; count++; p = strtok(null," "); } return count; } 13

Problem 3 Tips Part 1 Header: #include <pthread.h> Usage of pthread: // create a thread 't1' pthread_t t1; rc = pthread_create (&t1, &attr1(null), my_function, &my_arguments1); // join thread 1 and wait for completion void* status1; rc = pthread_join (t1, &status1); if (rc) { printf("error; return code from pthread_join(t1) is %d\n", rc); exit(-1); } 14

Problem 3 Tips Part 2 Header: #include <semaphore.h> sem_t: structure of semaphore sem_t sem; sem_init: initialize semaphore sem_init(&sem,0,1); // set up a binary semaphore to act as a mutex for the critical section "use of shovel", initialize it to 1 sem_destroy: destroy a semaphore sem_destroy(&sem); sem_wait(&sem) wait for/block on a semaphore decrease the value of the semaphore, and if the value is <0, it will BLOCK the thread sem_post(&sem) signal/post on a semaphore increase the value of the semaphore, and if the value becomes <= 0, the OS will unblock a blocked thread (a thread who called sem_wait and was blocked) 15

Problem 4 Cooks, Cashiers, and Customers are each modeled as a thread. Cashiers sleep until a customer is present. A Customer approaching a cashier can start the order process. A Customer cannot order until the cashier is ready. Once the order is placed, a cashier has to get a burger from the rack. If a burger is not available, a cashier must wait until one is made. The cook will always make burgers and place them on the rack. The cook will wait if the rack is full. There are NO synchronization constraints for a cashier presenting food to the customer. 16

Burger Buddies Problem 17

Prepare Knowledge: tips Simulating time passing sleep(rand() % NUM); Assigning ID id_cook[i] = i +1; create_thread (Cook, &id_cook[i]); void *Cook(void *args){ int id = *(int *)args; } 18

General Requirement Source file: BurgerBuddies.c Executable file: BBC gcc BurgerBuddies.c o BBC lpthread Run: BBC #Cooks #Cashiers #Customers #RackSize 19

Sample Output >./BBC 2 4 41 10 Cooks [2], Cashiers [4], Customers [41] Begin run. Cook [1] put a burger on the rack. Cook [1] put a burger on the rack. Cook [2] put a burger on the rack. Customer [10] come. Casher [3] accepts an order. Casher [3] take a burger to customor. Customer [19] come. Casher [2] accepts an order. Casher [2] take a burger to customor. Customer [7] come. Casher [3] accepts an order. Casher [3] take a burger to customor. Customer [17] come. Casher [2] accepts an order. Cook [1] put a burger on the rack. Casher [2] take a burger to customor. 20