linux下的进程,Linux下的进程

1、查看进程:ps -af(系统进程:ps -ax)

#include

#include

int main()

{

printf("Running ps with system!\n");

system("ps -ax");

printf("Done!\n");

return 0;

}

2、替换进程映像exec:

#include

int main()

{

printf("Starting!\n");

execlp("ps", "ps", "-ax", 0);

printf("Done!\n");

}

3、复制进程映像fork:

#include

#include

#include

int main()

{

pid_t pid;

char *message;

int n;

printf("fork program starting!\n");

pid = fork();

switch (pid)

{

case -1:

perror("fork failed!");

exit(1);

case 0:

message = "This is the child!\n";

n = 5;

break;

default:

message = "This is the parent!\n";

n = 3;

break;

}

for (; n > 0; n--)

{

puts(message);

sleep(1); // usleep(1000); // 1 second

}

exit(0);

}

#include

#include

#include

int main()

{

pid_t pid;

char *message;

int n;

int exit_code = 0;

printf("fork program starting!\n");

pid = fork();

switch (pid)

{

case -1:

perror("fork failed!");

exit(1);

case 0:

message = "This is the child!\n";

n = 5;

exit_code = 34;

break;

default:

message = "This is the parent!\n";

n = 1;

exit_code = 55;

break;

}

for (; n > 0; n--)

{

puts(message);

sleep(1); // usleep(1000); // 1 second

/* This section of the program waits for the child process to finish. */

if (pid)

{

int stat_val;

pid_t child_pid;

child_pid = wait(&stat_val);

printf("Child has finished:PID = %d\n", child_pid);

if (WIFEXITED(stat_val))

{

printf("Child exit with code:%d\n", WEXITSTATUS(stat_val));

}

else

{

printf("Child terminated abnormally!\n");

}

}

}

exit(exit_code);

}

4、僵进程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值