SYSTEM CALLs AND APIs
MULTIPROCESSING:
fork();
exec( *parameter(s) );
execlp( *parameter(s) );
wait( *parameter(s) );
sleep( *parameter(s) );
FILE READING/WRITING:
read( *parameter(s) );
write( *parameter(s) );
open( *parameter(s) );
close( *parameter(s) );
IPC:
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
ftruncate(shm_fd, 4096);
mmap( *parameter(s) );
munmap( *parameter(s) );
PIPES:
pipe( *parameter(s) ); //ordinary/unnamed/anonymous pipes
mkfifo( *parameter(s) ); //named pipes
unlink( *parameter(s) );
PTHREADING:
pthread_create( *parameter(s) );
pthread_join( *parameter(s) );
pthread_exit( *parameter(s) );
pthread_cancel( *parameter(s) );
pthread_attr_init( *parameter(s) );
pthread_attr_setdetachstate( *parameter(s) );
pthread_attr_destroy( *parameter(s) );
signal( *parameter(s) );
*NOTE: For parameter(s) passing, check coding examples provided below.
CODING EXAMPLES
C Program Forking Separate Process
Operating System Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne ©2018
IPC POSIX Producer
Operating System Concepts – 10th Edition 3.30 Silberschatz, Galvin and Gagne ©2018
IPC POSIX Consumer
Operating System Concepts – 10th Edition 3.32 Silberschatz, Galvin and Gagne ©2018
Ordinary
Pipes
Operating System Concepts – 10th Edition 3.40 Silberschatz, Galvin and Gagne ©2018
Named
Pipes
Operating System Concepts – 10th Edition 3.43 Silberschatz, Galvin and Gagne ©2018
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void *helloWorld(void *vargp) {
sleep(1);
printf("Hello World \n");
return NULL;
}
int main() {
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, helloWorld, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
exit(0);
}
Operating System Concepts – 10th Edition 4.32 Silberschatz, Galvin and Gagne ©2018
#define NUM_THREADS 5
void *PrintHello(void *threadid) {
long tid;
tid = (long) threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[]) {
pthread_t threads[NUM_THREADS];
int rc; long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
Operating System Concepts – 10th Edition 4.34 Silberschatz, Galvin and Gagne ©2018
Joining Example (1)
Operating System Concepts – 10th Edition 4.36 Silberschatz, Galvin and Gagne ©2018
Joining Example (2)
Operating System Concepts – 10th Edition 4.37 Silberschatz, Galvin and Gagne ©2018
Joining Example (3)
Operating System Concepts – 10th Edition 4.38 Silberschatz, Galvin and Gagne ©2018
Detaching Threads (1)
Operating System Concepts – 10th Edition 4.43 Silberschatz, Galvin and Gagne ©2018
Detaching Threads (2)
Operating System Concepts – 10th Edition 4.44 Silberschatz, Galvin and Gagne ©2018
Fork-Join Parallelism
Operating System Concepts – 10th Edition 4.55 Silberschatz, Galvin and Gagne ©2018
Fork-Join Parallelism
Operating System Concepts – 10th Edition 4.56 Silberschatz, Galvin and Gagne ©2018
Signal Handling (Cont.)
Operating System Concepts – 10th Edition 4.61 Silberschatz, Galvin and Gagne ©2018