$ gcc -o thread thread.c -lpthread
#include <unistd.h> #include <sys/types.h> #include <sys/wait.h> main() { int pid; if ((pid = fork()) == 0) { execl("/bin/ls", "ls", "/", 0); } else { wait(&pid); printf("Exec finished\n"); } }
#include <unistd.h> #include <stdio.h> int main() { printf("Running ps with execlp\n"); execlp("ps", "ps", "-ax", 0); printf("Done.\n"); exit(0); }
#include <pthread.h> #include <stdio.h> /* Parameters to print_function. */ struct char_print_parms { /* The character to print. */ char character; /* The number of times to print it. */ int count; }; /* Prints a number of characters to stderr, as given by PARAMETERS, which is a pointer to a struct char_print_parms. */ void* char_print (void* parameters) { /* Cast the cookie pointer to the right type. */ struct char_print_parms* p = (struct char_print_parms*) parameters; int i; for (i = 0; i < p->count; ++i) fputc (p->character, stderr); return NULL; } int main () { pthread_t thread1_id; ....... struct char_print_parms thread1_args; ....... /* Create a new thread to print 30000 x's. */ thread1_args.character = 'x'; thread1_args.count = 30000; ....... /* Create a new thread to print 20000 o's. */ thread2_args.character = 'o'; ....... ....... /* Make sure the first thread has finished. */ pthread_join (thread1_id, NULL); /* Make sure the second thread has finished. */ ....... /* Now we can safely return. */ return 0; }