免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2545 | 回复: 2
打印 上一主题 下一主题

进程替换问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-02 18:09 |只看该作者 |倒序浏览
1.问题描述:
程序一开始开一个pthread,用以执行execvp(cmdtemp[0],&cmdtemp[0]);
程序接收client的连接,根据client 发过来的控制命令,关掉上一个线程用execvp开的一个进程spcaview,再重新开一个线程用以执行execvp(cmdtemp1[0],&cmdtemp1[0]);开另一个spcaview,最终完成线程中执行的内容的替换。最终目地其实为了实现在一个主程序A中,执行一个外部程序,给它传入参数,但是在这个外部程序执行过程中要能够控制它的开启与关闭。用fork感觉 不行,就用pthread做了如下更改,就出现了上面的问题。

期待问题解决中...


2.代码:
/* File: server.c */

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>


char* cmdtemp[6];
char* cmdtemp1[6];


int thread_flag = 0;//flag to exit pthread
//two pthread to start servfox process
void* thread_spcaview(void)
{
    execvp(cmdtemp[0],&cmdtemp[0]);
    printf("start spcaview-0 pthread ok \n");
    if(thread_flag)
    {
        pthread_exit(NULL);   
    }
}
void* thread_spcaview1(void)
{
    execvp(cmdtemp1[0],&cmdtemp1[0]);
    printf("start spcaview-1 pthread ok \n");   
    if(thread_flag)
    {
        pthread_exit(NULL);   
    }
}

int main(int argc, char **argv)
{
    int fd;
    int address_len;
    struct sockaddr_in address;
    pthread_t thread_id;
    int ret;
    cmdtemp[0] = "spcaview";
    cmdtemp[1] = "-l";
    cmdtemp[2] = "-d";
    cmdtemp[3] = "/dev/video0";
    cmdtemp[4] = "&";

    cmdtemp1[0] = "spcaview";
    cmdtemp1[1] = "-l";
    cmdtemp1[2] = "-d";
    cmdtemp1[3] = "/dev/video0";
    cmdtemp1[4] = "&";   

    //pid_t pid,pid2;
    /*************************   
    if((pid = fork())<0)
    {
        perror("cannot create the new process");
        return -1;
    }
    else if(pid ==0)
    {
        execvp(cmdtemp[0],&cmdtemp[0]);   
    }
    if(pid2>0)
    {
        pid =pid2;   
        printf("child pid = %d\n",pid);        
        //execvp(cmdtemp[0],&cmdtemp[0]);                                    
    }   
    //pid = system("/home/lyt/servfox -g -d /dev/video0 -l  -w 7070 &");
    ***************************/


    fd = socket(AF_INET, SOCK_DGRAM, 0);    //SOCK_DGRAM
   
    bzero(&address, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons(5555);
    address_len = sizeof(address);
    bind(fd, (struct sockaddr *)&address, address_len);

    ret = pthread_create(&thread_id,NULL,(void*)thread_spcaview,NULL);
    if(ret==-1)
    {
        //peeror("cannot create new thread");
        return -1;   
    }
   
    while(1) {
        struct sockaddr_in client_address;
        socklen_t len = sizeof(client_address);
        int n;
        char line[80];
        char cmd[20]="";   
        printf("waiting...\n");

        n = recvfrom(fd, line, 80, 0, (struct sockaddr *)&client_address, &len);
        //printf("server received %d:%s\n", n, line);
        if(n)
        {//kill the old process to switch the channel
            
            if(strcmp(line,"0")==0)
            {
               
                //printf("pid = %d\n",pid);
                //sprintf(cmd,"kill -9 %d",pid);
                //system(cmd);
                printf("exec::00000000000\n");
                //pid = system("spcaview -w 10.10.19.129:7070 &");
                //pid = system("/home/lyt/servfox -g -d /dev/video0 -l  -w 7070 &");
                /************************
                if((pid2 = fork())<0)
                {
                    perror("cannot create the new process");
                    return -1;
                }
                else if(pid2 ==0)
                {
                    execvp(cmdtemp[0],&cmdtemp[0]);   
                    printf("I am in child pid\n");
                }
                if(pid2>0)
                {
                    pid =pid2;   
                    system(cmd);
                    printf("child pid = %d\n",pid);        
                    //execvp(cmdtemp[0],&cmdtemp[0]);                                    
                }
                ***************************/
                thread_flag = 1;
                sleep(2);
                thread_flag = 0;                                
                ret = pthread_create(&thread_id,NULL,(void*)thread_spcaview,NULL);
                if(ret==-1)
                {
                    //peeror("cannot create new thread");
                    return -1;   
                }

            }
            else if(strcmp(line,"1")==0)
            {
                //printf("pid  = %d\n",pid);
                //sprintf(cmd,"kill -9 %d",pid);
                //system(cmd);            

                printf("exec::11111111111\n");
                //pid = system("/home/lyt/servfox -g -d /dev/video1 -l  -w 7070 &");
                /*******************************
                if((pid2 = fork())<0)
                {
                    perror("cannot create the new process");
                    return -1;
                }
                else if(pid2 ==0)
                {
                    execvp(cmdtemp[0],&cmdtemp[0]);   
                    printf("I am in child pid\n");
                }
                if(pid2>0)
                {
                    pid =pid2;
                    printf("child pid = %d\n",pid);   
                    //execvp(cmdtemp[0],&cmdtemp[0]);            
                }
                                
                *****************************************/
                thread_flag = 1;
                sleep(2);
                thread_flag = 0;                                
                ret = pthread_create(&thread_id,NULL,(void*)thread_spcaview1,NULL);
                if(ret==-1)
                {
                    //peeror("cannot create new thread");
                    return -1;   
                }               
            }
            else
            {
                printf("exec::no such dev\n");
            }
        }
        

        //send data to client
        //sendto(fd, line, n, 0, (struct sockaddr *)&client_address, len);
    }
}

论坛徽章:
0
2 [报告]
发表于 2009-02-06 23:39 |只看该作者
fork 是UNIX下建立进程的不二法门, 不管哪个fork都要fork.

论坛徽章:
0
3 [报告]
发表于 2009-02-07 09:48 |只看该作者
你是不是要编写一个类似于shell的程序?不过是接受网络上的用户输入的。对不?

控制开启与关闭

需要保存进程或者线程的ID,然后进程用kill函数,线程用pthread_cancel函数。

你可以找找介绍bashshell实现资料。

shell只管接受字符串。
执行的时候就fork一个新进程执行。
需要关闭它就借助kill函数。

线程的话就是pthread_cancel关闭。

用fork可以实现。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP