getpid : 获取当前进程id
getppid :获取当前进程父进程的id
include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf ("当前进程id: %d\n", getpid());
printf ("父进程 id: %d\n", getppid());
while(1);
return 0;
}
创建子进程 pid_t pid = fork();
返回值:1、在父进程中返回子进程的pid 2、在子进程中,返回0
创建成功以后,子进程和父进程的执行顺序是不固定的
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int count = 0;
int main1()
{
// 创建子进程
// 返回值:1、在父进程中返回子进程的pid 2、在子进程中,返回0
// 创建成功以后,子进程和父进程的执行顺序是不固定的
pid_t pid = fork();
if (pid > 0) // 父进程
{
printf ("我是父进程, id = %d\n", getpid());
}
else if (0 == pid) // 子进程
{
printf ("我是子进程,id = %d, ppid = %d\n", getpid(), getppid());
}
count++;
printf ("count = %d\n", count);
while (1);
return 0;
}
int main2()
{
fork();
fork();
fork();
while (1);
return 0;
}
int main3()
{
fork();
fork() && fork() || fork();
fork();
while (1);
return 0;
}
int main()
{
int i;
for (i = 0; i<10; i++)
{
pid_t pid = fork();
if (0 == pid)
break;
}
while (1);
return 0;
}
exec
#include <stdio.h>
#include <unistd.h>
int main1(int argc, char **argv)
{
printf ("开始文件复制......\n");
// execl("mycopy", "./mycopy", "1.ppt", "2.ppt", NULL);
execl("/bin/cp", "cp", "1.ppt", "2.ppt", NULL);
printf ("文件复制结束......\n");
return 0;
}
int main2(int argc, char **argv)
{
printf ("开始文件复制......\n");
// 对于非系统命令,要写出完整路径
execlp("/mnt/hgfs/code/171/21/mycopy", "./mycopy", "1.ppt", "2.ppt", NULL);
// 对于系统命令,可以不用谢完整路径,会自动去 path 指定的路径下找命令
// execlp("cp", "cp", "1.ppt", "2.ppt", NULL);
printf ("文件复制结束......\n");
return 0;
}
int main3(int argc, char **argv)
{
printf ("开始文件复制......\n");
char *buf[] = {"./mycopy", "1.ppt", "2.ppt", NULL};
execv("mycopy", buf);
printf ("文件复制结束......\n");
return 0;
}
int main(int argc, char **argv)
{
printf ("开始文件复制......\n");
// system("./mycopy 1.ppt 2.ppt");
system("ls --color=auto");
printf ("文件复制结束......\n");
return 0;
}
exit
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void func()
{
printf ("退出");
// exit(0);
_exit(0);
}
int main(int argc, char **argv)
{
func();
printf ("111111111111111111111111\n");
return 0;
}
僵尸进程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int i;
for (i = 0; i < 10; i++)
{
pid_t pid = fork();
if (0 == pid)
{
//printf ("子进程退出\n");
//exit(0);
break;
}
}
while (1);
return 0;
}
父进程比子进程先退出
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
// pid_t pid = fork();
// if (pid > 0)
// {
// exit(0);
// }
FILE *fp = fopen("tmp", "w");
if (NULL == fp)
{
perror("打开文件失败");
return -1;
}
while (1)
{
fputs("hello world", fp);
fflush(fp);
sleep(1);
}
fclose (fp);
return 0;
}
守护进程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
daemon(0, 0);//守护进程函数
FILE *fp = fopen("/mnt/hgfs/code/171/21/tmp1", "w");
if (NULL == fp)
{
perror("打开文件失败");
return -1;
}
while (1)
{
fputs("hello world", fp);
fflush(fp);
sleep(1);
}
fclose (fp);
return 0;
}
wait
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// wait: 阻塞性函数 ---> 等待子进程退出
int main1(int argc, char **argv)
{
pid_t pid = fork();
if (pid > 0) // 父进程
{
printf ("等待子进程退出\n");
pid_t id = wait(NULL);
printf ("有一个子进程,id = %d\n", id);
}
else if(pid == 0) // 子进程
{
while(1)
{
printf ("hello world\n");
sleep(2);
}
}
return 0;
}
int main(int argc, char **argv)
{
//创建十个子进程
int i;
pid_t pid;
for (i = 0; i < 10; i++)
{
pid = fork();
if (0 == pid)
break;
}
if (pid > 0)
{
while (1)
{
printf ("等待子进程退出\n");
int status;
pid_t id = wait(&status);
if (WIFEXITED(status))
printf ("该子进程是正常死亡\n");
else
printf ("该子进程是非正常死亡\n");
printf ("有一个子进程,id = %d\n", id);
}
}
else if(pid == 0) // 子进程
{
while(1)
{
printf ("[%d] : hello world\n", getpid());
sleep(10);
}
}
return 0;
}