0% found this document useful (0 votes)
20 views16 pages

OS Coding Mid2

The document outlines various system calls and APIs related to multiprocessing, file reading/writing, inter-process communication (IPC), pipes, and pthreading in C programming. It includes examples of code demonstrating forking processes, creating threads, and using pipes. Additionally, it references the 10th edition of 'Operating System Concepts' by Silberschatz, Galvin, and Gagne.

Uploaded by

k230668
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views16 pages

OS Coding Mid2

The document outlines various system calls and APIs related to multiprocessing, file reading/writing, inter-process communication (IPC), pipes, and pthreading in C programming. It includes examples of code demonstrating forking processes, creating threads, and using pipes. Additionally, it references the 10th edition of 'Operating System Concepts' by Silberschatz, Galvin, and Gagne.

Uploaded by

k230668
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like