CSE 380: Homework 2: Synchronization

Similar documents
Shared Memory (8A) Shared Memory

경희대학교컴퓨터공학과 조진성. UNIX System Programming

OS Lab Tutorial 1. Spawning processes Shared memory

UNIT III- INTER PROCESS COMMUNICATIONS Part A

Gabrielle Evaristo CSE 460. Lab Shared Memory

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

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

INTER-PROCESS COMMUNICATION. UNIX Programming 2015 Fall by Euiseong Seo

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

Part II Processes and Threads Process Basics

Shared Memory Semaphores. Goals of this Lecture

Shared Memory Memory mapped files

Shared Memory. By Oren Kalinsky

COP 4604 UNIX System Programming IPC. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Lecture 8: Inter-process Communication. Lecturer: Prof. Zichen Xu

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

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

Process Synchronization

Inter-process communication (IPC)

CSPP System V IPC 1. System V IPC. Unix Systems Programming CSPP 51081

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

COMP 3100 Operating Systems

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications

CS 3305 Intro to Threads. Lecture 6

Multicore and Multiprocessor Systems: Part I

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

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

CSci 4061 Introduction to Operating Systems. IPC: Message Passing, Shared Memory

CS 550 Operating Systems Spring Inter Process Communication

Thread Fundamentals. CSCI 315 Operating Systems Design 1

Process Synchronization(2)

POSIX PTHREADS PROGRAMMING

컴퓨터특강 (UNIX System Programming) APUE(Interprocess Communication) [Ch. 14]

1 Architectural Support

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

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

Threads. Jo, Heeseung

Pipes. FIFOs. System V IPC. Message Queues. Shared Memory. Semaphores. APUE (Interprocess Communication. Page 2

Signal Example 1. Signal Example 2

UNIX IPC. Unix Semaphore Unix Message queue

PThreads in a Nutshell

Concurrent Programming. Concurrent Programming with Processes and Threads

Chapter 4 Concurrent Programming

POSIX Threads. HUJI Spring 2011

Reading compiler errors

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

Advanced Operating Systems : Exam Guide. Question/Material Distribution Guide. Exam Type Questions

Concurrency, Thread. Dongkun Shin, SKKU

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

2 UNIX interprocess communications

struct ipc_perm sem_perm; ushort sem_nsems; /* count of sems in set */ time_t sem_otime; /* last operation time */

LSN 13 Linux Concurrency Mechanisms

Project 4: Synchronization

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

CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization

Compile the Hello World program

Operating Systems, Assignment 2 Threads and Synchronization

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

CS510 Operating System Foundations. Jonathan Walpole

518 Lecture Notes Week 5

Operating systems fundamentals - B06

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

CS 385 Operating Systems Spring 2013 Homework Assignment 2 Third Draft Inter-Process Communications and Synchronization

CS 105, Spring 2007 Ring Buffer

CS510 Operating System Foundations. Jonathan Walpole

pthreads CS449 Fall 2017

UNIT III- INTERPROCESS COMMUNICATION

Operating Systems 2010/2011

Message Queues, Semaphores, Shared Memory

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

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

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

Reminder. COP4600 Discussion 8 Segmentation, File System, Fork() Implementation. Question-1. Discussion 7 Recap. Question-1(cont) Question-1

Mixed-Mode Programming

CS 261 Fall Mike Lam, Professor. Threads

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

Interprocess Communication. Bosky Agarwal CS 518

Introduction to PThreads and Basic Synchronization

CS333 Intro to Operating Systems. Jonathan Walpole

TCSS 422: OPERATING SYSTEMS

518 Lecture Notes Week 3

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

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

Lecture 19: Shared Memory & Synchronization

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

COMP 2355 Introduction to Systems Programming

Programming with Shared Memory. Nguyễn Quang Hùng

Systems Programming/ C and UNIX

Operating Systems. No. 5 ศร ณย อ นทโกส ม Sarun Intakosum

Lab 5: Inter-Process Communication

Programming with Shared Memory PART I. HPC Fall 2010 Prof. Robert van Engelen

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

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

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

OPERATING SYSTEMS 3rd Homework

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CS 6400 Lecture 11 Name:

ECE 598 Advanced Operating Systems Lecture 23

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

Transcription:

CSE 380 Homework 2 1 CSE 380: Homework 2: Synchronization Due : Thursday, October 2, 2003 Submit a hardcopy solution of the problems in class on Oct 2, and submit code and documentation for the programs using the turnin command to the account cse380@eniac by 11.59pm on Oct 2. The code should include comments which adequately describe the solution, as described below. Reminder: Copying the solution from a fellow student, a website, or some other source, is a violation of the University policies on academic integrity. Implementing Dining Philosophers Recall the dining philosophers problem of philosophers seated around a table eating spaghetti periodically. In this question, you have to implement a solution using system calls for semaphores. You will submit two variations of the dining philosophers problem: a) a solution to the dining philosophers problem using processes, shared memory, and semaphores b) a solution using threads and semaphores. A skeleton for each part is available at: http://www.cis.upenn.edu/~cse380/hw2.tar.gz It specifies the basic routines that need to be implemented. The skeleton includes a makefile that will allow you to type make proc to generate the part one and make thread to generate the program for part two. Please do not change the name of the files or the targets in the makefile. This assignment uses POSIX semaphores, Solaris shared memory functions, and POSIX threads. You are expected to use these calls for your programs. As far as the solution to the problem is concerned, you can use class notes, textbook, or design your own solution. Part One: Using Process and Shared Memory For part one, your solution should work in the following way (see dinphil proc.c in the skeleton): 1. Create one process for each philosopher 2. The number of philosophers should be the first argument to your program. The input range is restricted from three to 20 philosophers, inclusive. 3. Each philosopher should eat bites times, which is the second input parameter to the program. Count the number of times each philosopher eats, and when (s)he has eaten the maximum number of bites, (s)he stops eating (the corresponding process exits).

CSE 380 Homework 2 2 4. For mutual exclusion, use POSIX semaphores. Relevant system calls are: sem init(), sem post(), sem wait, sem getvalue and sem destroy(). The semaphores should be allocated in shared memory (see Appendix A on the system support for doing this in Solaris). 5. Simulate thinking and eating using the random and sleep system calls. 6. Your code should be well documented with sufficient explanation of high-level objectives as well as of details. Your documentation will serve as the main justification for the correctness of your program. It is your responsibility to convince the graders that your solution makes sense. Part Two: Using Threads Threads should be created using POSIX threads (man pthreads on eniac. An example of a creating pthreads is shown in Appendix B. Part two has the same basic requirements for part one with the following exceptions: 1. Create one thread for each philosopher instead of a process for each. 2. Shared memory is not required. 3. Mutual exclusion for the pthreads version can be implemented using either semaphores as in part one or by using pthread mutex lock and pthread mutex unlock. Submission We have created specific requirements for the naming of your source files, your program, the output, and the compilation process. Please do not remove targets from the makefile, or alter the names of the source files. Your hardcopy should divided into two sections, one for each part of the assignment. Separately staple each section and include your name, email address, and a designation of the part of the assignment at the beginning of each of the two sections. Please include an explanation before your code to explain your solution. use turnin with the following syntax: turnin -ccse380 -phw2 Makefile dinphil_thread.c dinphil_proc.c For every critical section in your code, add statements that identify the entering and exiting from that section. If your program has two critical sections, then the lock of the mutex before the first should be directly followed by the line: printf("starting critical section 1\n"); Unlocking the mutex to exit the critical section should be directly preceded by this line: printf("stopping critical section 1\n");

CSE 380 Homework 2 3 Use the exact format above. When philosopher i takes their forks, print a line with the following format to stdout: printf("philosopher %d taking forks\n", i); Similarly, when philosopher i puts their forks down, print a line with the following format: printf("philosopher %d putting forks\n", i); Keep track of each time a philosopher eats. When philosopher i eats for the j -th time, print the followinng: printf("philosopher %d eating for time %d\n", i, j); Use the man pages (as well as class notes and textbook) for information on necessary system calls (e.g. man semaphore and man pthreads).

CSE 380 Homework 2 4 Appendix A: Shared Memory System Calls This section gives information on how Unix processes can request and use shared memory segments. For every shared memory segment, the kernel maintains the following structure of information: /* * There is a shared mem id data structure (shmid_ds) for each * segment in the system. */ struct shmid_ds { struct ipc_perm shm_perm; /* operation permission struct */ size_t shm_segsz; /* size of segment in bytes */ /*... some implementation dependent info */ pid_t shm_lpid; /* pid of last shmop */ pid_t shm_cpid; /* pid of creator */ shmatt_t shm_nattch; /* current # attached */ ulong_t shm_cnattch; /* in-core # attached */ time_t shm_atime; /* last shmat time */ time_t shm_dtime; /* last shmdt time */ time_t shm_ctime; /* last change time */ ; The ipc perm structure contains the access permissions for the shared memory segment. struct ipc_perm { uid_t uid; /* owner s user id */ gid_t gid; /* owner s group id */ uid_t cuid; /* creator s user id */ gid_t cgid; /* creator s group id */ mode_t mode; /* access modes */ uint_t seq; /* slot usage sequence number */ key_t key; /* key */ ; int shmget(key_t key, int size, int shmflag); The shmflag argument specifies the low-order 9 bits of the mode for the shared memory, and whether a new segment is being created or if an existing one is being referenced. The shmflag argument is a combination of the constants:

CSE 380 Homework 2 5 Numeric Symbolic Description 0400 SHM R Read by owner 0200 SHM W Write by owner 0040 SHM R>>3 Read by group 0020 SHM W>>3 Write by group 0004 SHM R>>6 Read by world 0002 SHM W>>6 Write by world IPC CREAT See below IPC EXCL See below The rules for whether a new shared memory segment is created or whether an existing one is referenced are: Specifying a key of IPC PRIVATE guarantees that a unique IPC channel is created. Setting the IPC CREATE bit of the shmflag word creates a new entry for the specified key, if it does not exist. If an existing entry is found, that entry is returned. Setting both the IPC CREATE and IPC EXCL bits of the flag word creates a new entry of the specified key, only if the entry does not already exist. If an existing entry is found, an error occurs, since the IPC channel already exists. Upon creation, the data structure associated with the new shared memory identifier is initialized as follows: The values of shm perm.cuid, shm perm.uid, shm perm.cgid, and shm perm.gid are set equal to the effective user ID and effective group ID, respec- tively, of the calling process. The access permission bits of shm perm.mode are set equal to the access permission bits of shmflg. shm segsz is set equal to the value of size. The values of shm lpid, shm nattch shm atime, and shm dtime are set equal to 0. The shm ctime is set equal to the current time. The following example illustrates how shared memory can be used. #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> int main (int argc, char *argv[]){ int shmid; /* shared memory ID */ char *p; /* pointer to shared memory area */ /* reserve a 10-byte physical memory segment using the pid as the key */

CSE 380 Homework 2 6 shmid = shmget ((key_t) getpid (), 10, 0666 IPC_CREAT); if (shmid == -1) { puts ("shmget failed"); exit (1); /* attach the shared memory for use by this program */ p = (char *) shmat (shmid, (char *) 0, 0); strcpy (p, "hello"); /* put string into shared memory */ puts (p); /* detach the shared memory */ shmdt (p); /* remove the shared memory */ shmctl (shmid, IPC_RMID, 0);

CSE 380 Homework 2 7 Appendix B: Pthreads Example: thread test.c This program creates two pthreads which share the variable counter. Threads are created using pthread create which is passed, among other arguments, a pointer to a function that the thread should begin execution at as well as a pointer to an optional argument to that function. For a full explanation, see man pthread create. In this case, the function that each thread will begin execution at is go() and the argument to the function is a pointer to an element in the array arg. Similar to wait() for process, join() will wait for the specified thread to exit. For a general overview of pthreads, see man pthreads and for specific functions, see the appropriate man page. Semaphores can be used in pthreads and simple mutexes can be created as shown below. In this example, the variable counter must be protected by a mutex because two different threads are incrementing the value. Without the mutex, the potential for the counter to be incorrectly incremented exists. Although the probability of this occurrence is quite low in this example, as more threads are used and more data is shared, the probability will obviously increase. /* * compile with: "gcc -o thread_test thread_test.c -lpthread" */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <pthread.h> #include <errno.h> #define NLOOP 5000 int counter; pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER; void *go(void *); main() { pthread_t tida, tidb; int arg[2]; arg[0] = 1; arg[1] = 2; pthread_create(&tida, NULL, &go, (void *) &arg[0]); pthread_create(&tidb, NULL, &go, (void *) &arg[1]); /* wait for threads to stop */ pthread_join(tida, NULL);

CSE 380 Homework 2 8 pthread_join(tidb, NULL); exit(0); void * go(void *vptr) { int i,val; val = *(int *)vptr; printf("thread %d s argument is %d\n", pthread_self(), val); pthread_mutex_lock(&counter_mutex); for(i=0; i<nloop; i++) { counter++; printf("i = %d: id = pthread_mutex_unlock(&counter_mutex); %d : counter = %d\n", i, pthread_self(), counter);