Content
- Process-Related Unix System Calls
- Posix Thread
Process-Related Unix System Calls
- A processin Unix consistes of anaddress space& athread
- API- getpid(),- getppid()- Pid identifies address space&thread
 
- Pid identifies 
- fork(),- execv()- Create processes
 
- Create 
- exit(),- wait()- Terminate processes& synchronize with terminatingprocess
 
- Terminate 
- kill(),- sigaction()- Communicate with another processviasignals
 
- Communicate with another 
 
- fork()- Create - childe processfrom- parent processwith an identical copy of:- Thread state
- Address space
 - int n = 5; int pid = fork(); if (pid == 0) { // run child code n = n + 1; } else { // pid value > 0 // run parent code n = n - 1; }
 
- execv()- Replaces current - processwith a new- program:- Loaded from disk
- Code&- datacopied from disk
- Stackinitialized with activation frame of- main()
- Processor registersreinitialized
- Pidstays the same
- Doesn't return
 - char *cmd = "/bin/ls"; char *arg1 = "-l"; char *args[3]; args[1] = arg1; args[2] = NULL; execv(cmd, args); // code doesn’t execute
- fork()+- execv()can run a new program as a new process with the old one kept
 
- exit(retval)- Terminate itself:- Address spacedestroyed
- Process-specific OS state destroyed (e.g. open files)
- retvalsaved & return to- parent process- For parent processto learn aboutchild process
 
- For 
- Thread statedestroyed
 
 
- Terminate itself:
- wait()- Wait for child processto terminate; synchronize withchild processes
- Returns retvalreturned fromexit()
- 4 cases: - Wwait starts,- Ccontinue,- Eexit starts,- Dexit done- # Semaphore required for 1, 2, 3 1. down(c) up(p) Parent ---> W C-----> / \ Child --------> E D up(c) down(p) 2. Parent --------> WC-----> / \ Child ------> E | D # OS keeps child exit retval until wait() is issued by parent, (zombie process) # or destroyed when parent exits 3. Parent --------> E / \ Child -----> E D 4. Parent ---> E Child --------> ED
- Each child processneeds a pair ofsemaphore
 
 
- Wait for 
- kill()- Process send signals to itself or other processesby callingkillsystem call
- Receiver executes signal handler; if not setup, forced to exit
- Receiver process exits when scheduled to run next- Because if receiver is holding the lock, it must be released by itself instead of other threads
 
 
- Process send signals to itself or other 
- sigaction()- Setup signal handlerfunction
 
- Setup 
Posix Thread
- Allows creating additional threads in a Unix process
- API- pthread_create(thread, attr, start_routine, arg)
- pthread_exit(status)- Returns statusto a joining thread
 
- Returns 
- pthread_join(thread_id, status)- Block thread until thread with thread_idterminates
 
- Block thread until thread with 
- pthread_yield()
 
- Synchronization API - Mutex - pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mut); pthread_mutex_unlock(&mut)
- Monitor - int x,y; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_lock(&mut); while (x <= y) { pthread_cond_wait(&cond, &mut); } /* operate on x and y */ pthread_mutex_unlock(&mut);- pthread_mutex_lock(&mut); /* modify x and y */ if (x > y) pthread_cond_signal(&cond); pthread_mutex_unlock(&mut);
- Semaphores - sem_t sem name; sem_init(&sem_name, 0, 0); // (_, flag, init value) sem_wait(&sem_name); sem_post(&sem);