山东大学 操作系统实验 1.进程控制实验

本文介绍了山东大学操作系统实验的第一个任务,即实现一个父进程创建并控制子进程每隔3秒打印当前目录文件列表的程序。提供了相关源代码及编译运行结果。
摘要由CSDN通过智能技术生成

实验题目

编写一个父子协作进程,父进程创建一个子进程并控制它每隔 3 秒显示一次当前目录中的文件名列表。

实验代码

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("%d Process continue\n",getpid());
     }

pctl.c

#include "pctl.h"
int main( int argc,char* argv[])
{
    int t;
    int pid;
    int status;
    int childpid;
    char *args[]={"/bin/ls","-a",NULL};
    signal(SIGUSR1,(sighandler_t)sigcat);
    pid=fork();
    if(pid<0)
    {
        printf("Create Process fail!\n");
    }
    if(pid==0)
    {
        while ((1))
        {
            pause();
            childpid=fork();
            if(childpid<0)
                exit(EXIT_FAILURE);
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值