Linux System Programming note 5—— Process Management

1. The four program -- be the init process
     /sbin/init
     /etc/init
     /bin/init
     /bin/sh
2. /proc/sys/kernel/pid_max   -----32768
3. Obtaining the Process ID and Parent Process ID
#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);
4. The Exec Family of Calls
#include <unistd.h>

int execl (const char *path, const char *arg, ...); // The list of arguments must be NULL-terminated.

#include <unistd.h>

int execlp (const char *file,
                const char *arg,
                ...);

int execle (const char *path,
                const char *arg,
                ...,
                char * const envp[]);

int execv (const char *path, char *const argv[]);
int execvp (const char *file, char *const argv[]);
int execve (const char *filename,
                char *const argv[],
                char *const envp[]);


5. The fork() System Call
#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);


6.  fork plus exec
pid_t pid;

pid = fork();
if (pid == -1)
     perror("fork");

if (!pid){
     const char *args[] = {"windlass", NULL};
     int ret;

     ret = execv("/bin/windlass", args);
     if (ret == -1){
          perror("execv");
          exit(EXIT_FAILURE);
     }
}


7. vfork()
#include <sys/types.h>
#include <unistd.h>

pid_t vfork(void);

8. Terminate a Process
#include <stdlib.h>

void exit(int status);

Before terminating the process, the c library performs the following shutdown steps, in order:
1. Call any functions registered with atexit() or on_exit(), in the reverse order of their registration. 
2. Flush all open standard I/O streams
3. Remove any temporary files created with the tmpfile() function.

exit() invokes the system call _exit() to let the kernel handle the rest of the termination process:
#include <unistd.h>

void _exit(int status);

Note, however, that vfork() users should call _exit(), and not exit().

9. atexit()
#include <stdlib.h>

int atexit(void (*function)(void));
10. on_exit()
#include <stdlib.h>

int on_exit(void (*function)(int, void *), void *arg);

void my_function(int status, void *arg);

11. Waiting for Terminated Child Processes

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

pid_t wait(int *status);

#include <sys/wait.h>

int WIFEXITED(status);
int WIFSIGNALED(status);
int WIFSTOPPED(status);
int WIFCONTINUED(status);

int WEXITSTATUS(status);
int WTERMSIG(status);
int WSTOPSIG(status);
int WCOREDUMP(status);

12. Waiting for a Specific Process
#include <sys/types.h>
#include <sys/wait.h>

pid_t waitpid(pid_t pid, int *status, int options);

13. Even More Waiting Versatility

#include <sys/wait.h>

int waitid(idtype_t idtype,
              id_t id,
              siginfo_t *infop,
              int options);
idtype:
P_PID
P_GID
P_ALL

14. BSD Wants to Play: wait3() and wait4()

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

pid_t wait3(int *status,
                 int options,
                 struct rusage *rusage);

pid_t wait4(pid_t pid,
                 int *status,
                 int options,
                 struct rusage *rusage);

#include <sys/resource.h>

struct usage {
     struct timeval ru_utime;
     struct timeval ru_stime;
     long ru_maxrss;
     long ru_ixrss;
     long ru_idrss;
     long ru_isrss;
     long ru_minflt;
     long ru_majflt;
     long ru_nswap;
     long ru_inblock;
     long ru_oublock;
     long ru_msgsnd;
     long ru_msgrcv;
     long ru_nsignals;
     long ru_nvcsw;
     long ru_nivcsw;
};

15. Launching and Waiting for a New Process
#include  _XOPEN_SOURCE
#include <stdio.h>

int system(const char *command);

16. There are for user IDs associated with a process: The real, effective, saved, and filesystem user IDs.

By executing a setuid(suid) binary, the process can change its effective user ID.

17. Changing the Real or Saved User or Group ID
#include <sys/types.h>
#include <unistd.h>

int setuid(uid_t uid);
int setgid(gid_t gid);

18. Changing the Effective User or Group ID
#include <sys/types.h>
#include <unistd.h>

int seteuid(uid_t euid);
int setegid(gid_t egid);

19. Changing the User and Group IDs, BSD Style
#include <sys/types.h>
#include <unistd.h>

int setreuid(uid_t ruid, uid_t euid);
int setregid(gid_t rgid, gid_t egid);

20. Changing the User and Group IDs, HP-UX Style
#include _GNU_SOURCE
#include <unistd.h>

int setresuid(uid_t ruid, uid_t euid, uid_t suid);
int setresgid(gid_t rgid, gid_t egid, gid_t sgid);

21. Obtaining the User and Group IDs
#include <unistd.h>
#include <sys/types.h>

uid_t getuid(void);
gid_t getgid(void);

uid_t geteuid(void);
gid_t getegid(void);

22. Session System Calls
#include <unistd.h>

pid_t setsid(void);


Obtaining the current session ID:
#define _XOPEN_SOURCE 500
#define <unistd.h>

pid_t getsid(pid_t pid);

23. Process Group System Calls
#define _XOPEN_SOURCE 500
#include <unistd.h>

int setpgid(pid_t pid, pid_t pgid);

pid_t getpgid(pid_t pid);

#include <unistd.h>

int daemon (int nochdir, int noclose);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值