操作系统实验一之进程控制实验
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
// 编写一个多进程并发执行程序。父进程首先创建一个执行 ls 命令的子进程然后再创建一个执行 ps 命令的子进程,并控制ps 命令总在 ls 命令之前执行。
pctl.c
#include "pctl.h"
int main(int argc,char *argv[])
{
int i;
int pid1;
int pid2;
int status1;
int status2;
signal(SIGINT,(sighandler_t)sigcat);
char *args1[] = {"/bin/ls","-l",NULL};
char *args2[] = {"/bin/ps","-l",NULL};
pid1 =fork();
if(pid1<0)
{
printf("Create Process 1 fail\n");
exit(EXIT_FAILURE);
}
else if(pid1==0)
{
printf("I am Child one process %d \nMy father is %d\n",getpid(),getppid());
pause();
for(i =0; args1[i]!=NULL; i++)
{
printf("%s",args1[i]);
}
printf("\n");
status1 =execve(args1[0],args1,NULL);
}
else
{
printf("\nI am Parent process %d\n",getpid());
pid2 =fork();
if(pid2<0)
{
printf("Create Process 2 fail\n");
exit(EXIT_FAILURE);
}
else if(pid2 ==0)
{
printf("I am Child two process %d,\nMy father is %d\n",getpid(),getppid());
for(i =0; args2[i]!=NULL; i++)
{
printf("%s",args2[i]);
}
printf("\n");
status2 = execve(args2[0], args2, NULL);
}
else
{
waitpid(pid2, &status2, 0);
kill(pid1, SIGINT);
}
}
return EXIT_SUCCESS;
}
pctl.h
#include<sys/types.h>
#include<wait.h>
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
//进程?定义的键盘中断信号处理函数
typedef void(*sighandler_t) (int) ;
void sigcat(){
printf("%dProcesscontinue\n",getpid());
}
运行结果:
I am Parent process 2353
I am Child one process 2354
I am Child two process 2355,
My father is 2353
My father is 2353
/bin/ps-l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 2298 2286 6 80 0 - 2192 wait pts/0 00:00:00 bash
0 S 1000 2353 2298 0 80 0 - 502 wait pts/0 00:00:00 pctl
1 S 1000 2354 2353 0 80 0 - 502 pause pts/0 00:00:00 pctl
0 R 1000 2355 2353 0 80 0 - 657 - pts/0 00:00:00 ps
2354Processcontinue
/bin/ls-l
cqc@cqc-virtual-machine:~$ total 19168
-rw-rw-r-- 1 cqc cqc 974 May 9 08:00 <invalid path>
-rw-r--r-- 1 cqc cqc 8445 Apr 5 23:42 examples.desktop
drwxrwxr-x 15 cqc cqc 12288 Apr 6 02:09 libxml2-2.6.32
-rw-rw-r-- 1 cqc cqc 4722227 Apr 6 00:16 libxml2-2.6.32.tar.gz
-rw-rw-r-- 1 cqc cqc 163 May 9 06:54 makefile
-rw-rw-r-- 1 cqc cqc 0 May 9 06:54 makefile~
drwxrwxr-x 2 cqc cqc 4096 May 5 05:42 nimei
-rwxrwxr-x 1 cqc cqc 9003 May 9 08:22 pctl
-rw-rw-r-- 1 cqc cqc 1343 May 9 08:24 pctl.c
-rw-rw-r-- 1 cqc cqc 1005 May 9 07:33 pctl.c~
-rw-rw-r-- 1 cqc cqc 257 May 9 08:11 pctl.h
-rw-rw-r-- 1 cqc cqc 303 May 9 06:52 pctl.h~
-rw-rw-r-- 1 cqc cqc 4260 May 9 08:22 pctl.o
drwxr-xr-x 15 cqc cqc 4096 Apr 6 02:13 php-5.3.8
-rw-rw-r-- 1 cqc cqc 14789551 Apr 6 01:28 php-5.3.8.tar.gz
drwxrwxr-x 4 cqc cqc 4096 May 9 07:43 workspace
drwxr-xr-x 2 cqc cqc 4096 Apr 6 02:28 ??????
drwxr-xr-x 2 cqc cqc 4096 Apr 5 23:58 ?????????
drwxr-xr-x 2 cqc cqc 4096 Apr 5 23:58 ??????
drwxr-xr-x 2 cqc cqc 4096 Apr 6 01:56 ??????
drwxr-xr-x 2 cqc cqc 4096 Apr 5 23:58 ??????
drwxr-xr-x 2 cqc cqc 4096 Apr 5 23:58 ??????
drwxr-xr-x 2 cqc cqc 4096 Apr 5 23:58 ??????
drwxr-xr-x 2 cqc cqc 4096 Apr 5 23:58 ??????
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献9条内容
所有评论(0)