7.18多进程编程

使用多进程 + wait + exec + strtok 实现一个伪装的终端 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t; 
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;


char* getLine(char* buf,int size){
    fgets(buf,size,stdin);  //从标准输入流读取size-1个字节数据到buf这个字符数组里去
    int len = strlen(buf);  //计算字符数组buf的长度
    if(buf[len-1]=='\n'){   //最后一个字符为换行符时,把它变为0
        buf[len-1] = 0;
    }
}

int main(int argc, const char *argv[])
{
    while(1){
        int retval = fork();//获取fork的返回值
        if(retval > 0){     //若返回值大于0,进行进程资源回收
            wait(0);
        }else{
            char* username = getlogin(); //获取当前用户的登录名
            char hostname[64] = {0};
            char pwd[128] = {0};
            gethostname(hostname,64);    //获取本地主机的标准主机名
            getcwd(pwd,128);             //获取当前的工作目录
            printf("\033[1;32;10m%s@%s\033[0m:\033[1;34;10m%s\033[0m$ ",username,hostname,pwd);   //打印出模拟终端的效果
            fflush(stdout); //刷新标准输出流的缓存区




            char* arg[20] = {0};
            // find /usr/include -name stdio.h
            // cd ..
            char cmd[128] = {0};
            getLine(cmd,128); //调用getline函数,获取从终端输入的指令

            char* retval = NULL;
            int i = 0;
            do{
                if(retval == NULL){               //如果retval为NULL,将字符串cmd按空格分解成多个子串
                    retval = strtok(cmd," ");
                }else{
                    retval = strtok(NULL," ");
                }
               // printf("retval = %s\n",retval);
                arg[i++] = retval;
            }while(retval != NULL);

            if(strcmp(arg[0],"cd")==0){                //当字符串arg[0]和cd相等时,改变工作目录到arg[1]
                chdir(arg[1]);
            }else{
                execvp(arg[0],arg);                   //替换原先终端的进程到伪终端中
            
                printf("没有找到该指令\n");
            }
            //return 0;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值