linux c fork简单调用bin or shell script demo

// exec shell script

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
void hello()
{
  printf("hello word \n");
}

int main()
{
  pid_t pid;
  pid=fork();

  if(pid<0)
  {
    printf("fail to fork \n");
    exit(3);
  }
  else if(pid ==0)
  {

    // child process run  testcallexecl  
    //if(execl("/opt/test/testcallexecl","interp","arg1",NULL)<0)

   //  exec shell script /usr/bin/hello    must be set  x privilege to hello
    if(execl("/usr/bin/hello","xx","uu",NULL))
    {
       printf("fail to execl \n");
       exit(2);
    }
    printf("the child is not hello \n");
    exit(0);
  }
  printf("the parent %u\n",getpid());
  return 0;
}

 

cat /usr/bin/hello

  #!/bin/sh

  echo hello word

 

---------------------------------------------------------------------------------------------------

//  call bin

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
  pid_t pid;
  pid=fork();

  if(pid<0)
  {
    printf("fail to fork \n");
    exit(3);
  }
  else if(pid ==0)
  {

    //exec testpid exec bin file
    if(execvp("testpid",NULL)<0)
    {

       //child process exec
       printf("fail to exec \n");
       exit(2);
    }
    printf("the child is not exec \n");
    exit(0);
  }
  printf("the parent %u\n",getpid());
  return 0;
}

 

 cat pid.c
#include<stdio.h>
#include<unistd.h>

int main()

{
  pid_t pid,ppid,uid,euid,gid,egid;

  pid=getpid();
  ppid=getppid();

  uid=getuid();
  euid=geteuid();

  gid=getgid();
  egid = getegid();

  printf("id of current process :%u \n",pid);
  printf("parent id of current process :%u \n",ppid);
  printf("user id of current process :%u \n",uid);
  printf("effective user id of current process :%u \n",euid);
  printf("gid of current process :%u \n",gid);
  printf("effective gid of current process :%u\n",egid);

  // setx pid  set gid
  if(setuid(501)==-1)
   {
     perror("fail to set uid");
     exit(1);
   }
  uid=getuid();
  euid=geteuid();
  printf("id of current process :%u \n",pid);
  printf("parent id of current process :%u \n",ppid);
  return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值