您的位置:首页 > 运维架构 > Linux

linux 初学者之进程控制

2015-10-08 21:47 691 查看
1. 操作系统最核心的概念就是进程

2.进程是一个动态的实体,是程序的一次执行过程。进程是操作系统资源分配的基本单位。

3.线程在进程内部,她是比进程更小的能独立运行的基本单位,线程基本上不拥有系统资源,它与同属于一个进程的其他线程共享进程的全部资源。进程在执行过程中拥有独立的内存单元。其他内部的线程共享这些内存。一个线程可以创建和撤销另一个线程,同一个进程中的多个线程可以进行并行执行

4.每一个进程都有一个唯一的进程ID标识

5.linux进程由3部分组成,代码段,数据段,堆栈段

6.linux下C程序的生成分为4个阶段:预编译,编译,汇编,链接。编译器gcc经过预编译编译,汇编3个步骤将源程序文件转化为目标文件。如果程序有多个目标文件火灾个程序使用楼库函数,编译器还要将所有的目标文件或者所需要的库连接起来,最后生成可执行程序。当程序执行的时候,操作系统将可执行程序复制到内存中。

7.进程的内存映像是指内核在内存中如何存放可执行程序文件。在将程序转化为进程的过程中,操作系统将可执行程序有硬盘复制到内存中。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
pid_t pid;

printf("process creation study\n");
pid = fork();
switch(pid){
case 0:
printf("child process is running,CurPid is %d,Parent is %d\n",pid,getppid());
break;
case -1:
printf("process creation failed\n");
break;
default:
printf("parent process is running,Childpid is %d,Parentpid is %d\n",pid,getpid());
break;
}
// exit(-1);
return 0;
}

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
pid_t pid;
char * msg;
int k;

printf("process creation study\n");
pid = fork();
switch(pid){
case 0:
msg = "Child process is runing ";
k = 3;
break;
case -1:
perror("process creation failed\n");
break;
default:
msg = "parent process is runing";
k = 5;
break;
}
while(k > 0)
{
puts(msg);
sleep(1);
k--;
}
// exit(-1);
return 0;
}
~

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;

pid = fork();
switch(pid){
case 0:
while(1){
printf("a background process ,pid:%d,parentid:%d\n",getpid(),getppid());
sleep(3);
}
case -1:
perror("process creation failed\n");
exit(-1);
default:
printf("I am parent process,my pid is %d\n",getpid());
exit(0);
}

return 0;
}
~
~

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int globVar = 5;

int main(void){
pid_t pid;
int i;
int var = 1 ;
printf("fork is diffrent with vfork \n");

//pid = fork();
pid = vfork();
switch(pid){
case 0:
i = 3;
while(i-- > 0)
{
printf("Child process is running\n");
globVar++;
var++;
sleep(1);
}
printf("child's globVar = %d,var = %d\n",globVar,var);
break;
case -1:
perror("process creation failed\n");
break;
default:
i=5;
while(i-- > 0)
{
printf("parent process is running\n");
globVar++;
var++;
sleep(1);
}
printf("parent's globVar is %d,var = %d\n",globVar,var);
exit(0);
}

return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include <syslog.h>

int init_daemon(void)
{
int pid;
int i;

signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
signal(SIGHUP,SIG_IGN);

pid = fork();
if(pid > 0){
exit(0);
}
else if(pid<0){
return -1;
}
setsid();

pid = fork();
if(pid > 0){
exit(0);
}
else if(pid<0){
return -1;
}

for(i=0;i< NOFILE;close(i++));

chdir("/");

umask(0);

signal (SIGCHLD,SIG_IGN);

return 0;
-- 插入 --                                                                                                                                                     1,19         顶端

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

extern char **environ;

int main(int argc,char *argv[]){
int i;

printf("Argument:\n");
for(i=0;i<argc;i++){
printf("argv[%d] is %s\n",i,argv[i]);
}

printf("Enviroment:\n");

for(i = 0;environ[i]!=NULL;i++)
printf("%s\n",environ[i]);

return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc,char * argv[],char **environ){
int i;

printf("I am a process image!\n");
printf("My pid = %d, parentpid = %d\n",getpid(),getppid());
printf("uid = %d,gid = %d\n",getuid(),getgid());

for(i=0;i<argc;i++)
printf("argv[%d]:%s\n",i,argv[i]);

return 0;

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc,char *argv[],char **environ)
{
pid_t  pid;
int stat_val;
printf("Exec example@\n");
pid = fork();
switch(pid){
case -1:
perror("process creation failed\n");
exit(1);
case 0:
printf("child process is running\n");
printf("My pid =%d,parentpid = %d\n",getpid(),getppid());
printf("uid = %d,gid = %d\n",getuid(),getgid());
execve("processimage",argv,environ);
printf("process never go to here\n");
exit(0);
default:
printf("parent process is running \n");
break;

}
wait(&stat_val);
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: