操作系统实验一之进程控制实验

// 编写一个多进程并发执行程序。父进程首先创建一个执行 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 ??????



  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值