c语言多进程编程

Python017

c语言多进程编程,第1张

进程这个词用得比较少,听过来有点不熟悉。你这个程序在linux下应该很容易实行,就是个进程间通信的问题,管道、消息队列、共享内存都可以,可以找找相关资料。昨天失言不好意思。

三个源文件分别为1.c、2.c、3.c一个头文件share.h。

share.h:

//共享的内存,两个数组

typedef struct{

int a[2]

int b[2]

int id

}share_use

1.c:

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include"share.h"

int main(){

void *shared_memory = (void *)0

share_use *share_stuff

int shmid

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT)//创建共享内存

if(shmid==-1){

fprintf(stderr,"共享内存创建失败!\n")

exit(1)

}

shared_memory = shmat(shmid, (void *)0,0)//让进程可以访问共享内存

if(shared_memory == (void *)-1){

fprintf(stderr,"启用共享内存失败!\n)"

exit(1)

}

printf("Memory attached at %X\n",(int)shared_memory)

share_stuff = (share_use *)shared_memory

share_stuff->id=0

share_stuff->a[0]=1

share_stuff->a[1]=2

while(1){

if(share_stuff->id)

exit(0)

sleep(10)

}

}

2.c:

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include"share.h"

int main(){

void *shared_memory = (void *)0

share_use *share_stuff

int shmid

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT)//创建共享内存

if(shmid==-1){

fprintf(stderr,"共享内存创建失败!\n")

exit(1)

}

shared_memory = shmat(shmid, (void *)0,0)//让进程可以访问共享内存

if(shared_memory == (void *)-1){

fprintf(stderr,"启用共享内存失败!\n")

exit(1)

}

printf("Memory attached at %X\n",(int)shared_memory)

share_stuff = (share_use *)shared_memory

share_stuff->b[0]=share_stuff->a[0]*100

share_stuff->b[1]=share_stuff->a[1]*100

while(1)

{

if(share_stuff->id)

exit(0)

sleep(10)

}

}

3.c:

#include<unistd.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include"share.h"

int main(){

void *shared_memory = (void *)0

share_use *share_stuff

int shmid

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT)//创建共享内存

if(shmid==-1){

fprintf(stderr,"共享内存创建失败!\n")

exit(1)

}

shared_memory = shmat(shmid, (void *)0,0)//让进程可以访问共享内存

if(shared_memory == (void *)-1){

fprintf(stderr,"启用共享内存失败!\n")

exit(1)

}

printf("Memory attached at %X\n",(int)shared_memory)

share_stuff = (share_use *)shared_memory

printf("共享内存中有元素:%d , %d",share_stuff->b[0],share_stuff->b[1])

share_stuff->id=1

return 0

}

linux或unix环境下编译

/*请解释一下*/

while(((child=wait(&status))==-1)&(errno==EINTR))

这种的目的是父亲进程等待子进程结束,并回收子进程的资源,将子进程的退出状态存储在status中,同时,返回该子进程的pid。

如果wait函数返回-1表示wait函数被其它情况打断返回,并没有等待到子进程结束,而同时判断errno的值是不是EINTR(意思是让你try again),那么,让进程继续等待。因为这个错误并不是真正wait错误,而是被timeout时间等造成的,因此重新等待。而如果是其它情况,显然是wait函数调用错误,即下面的if(child==-1),需要打印错误信息。‘

//但你这句应该写错了。应该是逻辑与而不是位与操作。即

while(((child=wait(&status))==-1)&&(errno==EINTR))

/*请解释一下*/

if(child==-1)

你对信号处理部分还需要努力。

另外介绍一本书《Linux高级程序设计 第3版》 上面讲得很清楚。

有问题我们继续交流,一起学习。

更多技术文章可以关注我的微博,名字:成都睿尔科技 。

下面为C语言调用WIN API实现创建线程: 1,导入头文件 2,声明实现方法DWORD WINAPI ThreadProc1( LPVOID lpParam ) {} 3,在main()方法中调用 CreateThread(NULL,0 ,ThreadProc1,NULL,0,NULL)要注意的是主线程不能结束,如果主线程结束,则它的子线程也会被杀死。 #include #include #include DWORD WINAPI ThreadProc1( LPVOID lpParam ) { int i=0time_t timerwhile(1) { timer=time(NULL)printf("The current time is: %s\n",asctime(localtime(&timer)))sleep(1)} } void main() { int i=0//让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死” //创建线程1 CreateThread( NULL, // default security attributes 0, // use default stack size ThreadProc1, // thread function NULL, // argument to thread function 0, // use default creation flags NULL)// returns the thread identifier for() { } }