

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS414: Operating Systems
CS414: Operating Systems
CS414: Operating Systems
#include <unistd.h> #include <stdio.h> int main () { pid_t child_pid;
printf ("Hello\n"); child_pid = fork ();
if (child_pid == -1) { perror("fork"); exit(-1); } printf ("bye\n"); exit(0);
}
CS414: Operating Systems
CS414: Operating Systems
int main() { char prgname[1024]; pid_t pid; int status; if (fgets(prgname, 1024, stdin) == NULL) { printf ("Did not read program name. Aborting!\n"); exit(1); } prgname[strlen(prgname)-1]=0; if ((pid = fork ()) == -1) { perror("fork"); exit(1); } if (pid == 0) { execlp(prgname,prgname,0); printf("%d I did not find program %s\n", getpid(), prgname); exit (1); } else { waitpid(pid, &status, 0); if (status == 0) printf ("Program %s finished!\n", prgname); } return 0; } CS414: Operating Systems
CS414: Operating Systems
// Pthreads example code // compile with: gcc -Wall pthreads_example.c -o pthreads_example -lpthread // execute with: ./pthreads_example (or "time ./pthreads_example")
#include <pthread.h> #include <stdio.h>
void thread_func (int num) { long long i,j;
printf("Thread %d executing.\n", num); j = 0; for (i=0; i < 700000000U; ++i){ ++j; } printf("Thread %d done (%lld).\n", num, j); }
CS414: Operating Systems
int main () { pthread_t thread1, thread2; pthread_t thread3, thread4;
pthread_create(&thread1, NULL, (void *) &thread_func, (void *) 1); pthread_create(&thread2, NULL, (void *) &thread_func, (void *) 2); pthread_create(&thread3, NULL, (void *) &thread_func, (void *) 3); pthread_create(&thread4, NULL, (void *) &thread_func, (void *) 4);
pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL);
return 0;
}
CS414: Operating Systems