Process Control

Prev: System Call Error Handling
TOC: Exceptional Control Flow
Next: Signals

Obtaining Process IDs

#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);
							Returns: PID or either the caller or the parent

Creating and Terminating Processes

#include <stdlib.h>

void exit(int status);
#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);
						Returns 0 to child, PID of child to Parent -1 on error
int main(){
	pid_t pid;
	int x = 1;
	
	pid = Fork();
	if(pid = 0){//child
		printf("child: x = %d\n", ++x);
		exit(0);
	}
	//parent
	printf("parent: x=%d\n", --x);
	exit(0);
}

Reaping Child Process

#include <sys/types.h>
#include <sys/wait.h>

pid_t waitpid(pid_t pid, int *statusp, int options);
					Retunrs PID of child if OK, 0(If WHANG), or -1 on error

Putting Processes to Sleep

#include <unistd.h>

unsigned int sleep(insigned int secs);
													Retunrn secnds left to sleep

Loading and Running Programs

#include <unistd.h>

int exceve(const char *filename, const char *argv[], const char *envp[]);
									Does not return if OK, return -1 on error
int main(int argc, char **argv, char **envp);
#include <stdlib.h>

char *getenv(const char *name);
							Returns pointer to name if it exists, NULL if not
#include <stdlib.h?

int setenv(const char *name, const char *newvalue, int overwrite);
								Returns 0 on success -1 on error
void unsetenv(const char *name);
								Returns nothing

Prev: System Call Error Handling
TOC: Exceptional Control Flow
Next: Signals

Powered by Forestry.md