无法将当前进程 ID 转换为子进程 ID
Posted
技术标签:
【中文标题】无法将当前进程 ID 转换为子进程 ID【英文标题】:not able to shift current process id to child process id 【发布时间】:2014-06-24 04:35:03 【问题描述】:int _tmain(int argc, _TCHAR* argv[])
WinSockStartup();
//declarations
char data[255]=" server data",buff[255];
int len,port,ch,y=1,n=0, no_of_threads=0,CP_ret=0;//,Sel_ret=0,itr=0,max_fd;
struct sockaddr_in new_addr;
int sockfd,data_len_recv=-1,on=1,accfd=0,len_addr,data_len_send=-1;
//file writing attributes
char FileName[1024],logdata[1024];
SYSTEMTIME st;
//setting initial values
memset(&new_addr,0,sizeof(new_addr));
memset(&sockfd,0,sizeof(int));
memset(&accfd,0,sizeof(int));
memset(&len_addr,sizeof(struct sockaddr),sizeof(int));
memset(&port,0,sizeof(int));
memset(buff,0,sizeof(buff));
//create process atrribute
STARTUPINFO si;
PROCESS_INFORMATION pi_child,pi_parent,pi_current;
ZeroMemory( &si, sizeof(STARTUPINFO) );
si.cb = sizeof(STARTUPINFO);
ZeroMemory( &pi_child, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &pi_parent, sizeof(PROCESS_INFORMATION) );
//select attributes
struct timeval soctimeval;
soctimeval.tv_sec = 20;
soctimeval.tv_usec = 0;
fd_set read_fd_set;
FD_ZERO(&read_fd_set);
printf("enter the port number\n");
scanf("%d",&port);
new_addr.sin_family= AF_INET;
for(int y=0;y<7;y++)
new_addr.sin_zero[y]= 0;
new_addr.sin_port=htons(port);
new_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//get parent process id
pi_parent.dwProcessId= GetCurrentProcessId();
//socket making
sockfd =socket(new_addr.sin_family, SOCK_STREAM,IPPROTO_TCP);
if(sockfd==-1)
printf("error in opening socket %d \n",WSAGetLastError());
else
printf("Socket made Successfully\n");
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
//bind socket
if((bind(sockfd,(struct sockaddr *)&new_addr,sizeof(struct sockaddr)))==-1)
printf("bind error %d \n",WSAGetLastError());
else
printf("Binded Successfully\n");
//listen socket
if( listen(sockfd,10)==-1)
printf("Error in listening %d \n",WSAGetLastError());
else
printf("Listened Successfully\n");
while(1)
//accept
accfd= accept(sockfd,(struct sockaddr *)&new_addr,&len_addr);
if(accfd==-1)
printf("error in accept %d \n",WSAGetLastError());
else
printf("Accepted Successfully\n");
//creating child process for multiple clients
no_of_threads ++;
printf("client no: %d\n",no_of_threads);
CP_ret=CreateProcess(TEXT("C:\\Users\\richa-a\\Desktop\\create_process_multiclient1\\client_server\\Debug\\server_tcp.exe"),
NULL,
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
(BOOL)1, // Set handle inheritance to TRUE
DETACHED_PROCESS, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi_child );
pi_current.dwProcessId=GetCurrentProcessId(); //even after the create process. the current id is still of the parent.
if(pi_parent.dwProcessId==pi_current.dwProcessId || CP_ret==0)
printf("\nparent id is %d\n",pi_parent.dwProcessId);
printf("process id of child is %d \n",pi_child.dwProcessId);
printf("process id of current function is %d \n",pi_current.dwProcessId);
printf("child is nt running the code\n");
//pi_current.dwProcessId=GetCurrentProcessId();
if(pi_child.dwProcessId==pi_current.dwProcessId)
//opening file
GetLocalTime (&st);
sprintf(FileName,"D:\\Serve_Client\\Child_Process_%d_%d__%d.%d.%d.%d.log",st.wDay,st.wMonth,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
Child_FPtr=fopen(FileName,"wb");
sprintf(logdata,"logging start for client %d\n",no_of_threads);
LogInfo(logdata);
//fwrite(logdata,sizeof(logdata),sizeof(logdata),Child_FPtr);
sprintf(logdata,"process id of parent is %d\n",pi_parent.dwProcessId);
LogInfo(logdata);
sprintf(logdata,"process id of child is %d \n",pi_child.dwProcessId);
LogInfo(logdata);
sprintf(logdata,"process id of current function is %d \n",pi_current.dwProcessId);
LogInfo(logdata);
sprintf(logdata,"child is running the code\n");
LogInfo(logdata);
//recv data
data_len_recv=recv(accfd,(char *)buff, sizeof(buff)-1,0);
buff[data_len_recv]='\0';
sprintf(logdata,"%s",buff);
LogInfo(logdata);
if(data_len_recv==sizeof(buff))
sprintf(logdata,"data rev Successfully\n");
LogInfo(logdata);
sprintf(logdata,"Recv data: %s\n",buff);
LogInfo(logdata);
else if(data_len_recv<sizeof(buff))
sprintf(logdata,"hlf data rev Successfully\n");
LogInfo(logdata);
sprintf(logdata,"Recv data: %s\n",buff);
LogInfo(logdata);
else if(data_len_recv==-1 || data_len_recv==0)
sprintf(logdata,"error in recv %d \n",WSAGetLastError());
LogInfo(logdata);
//send data
data_len_send=send(accfd,(const char *)data, sizeof(data),0);
if(data_len_send==sizeof(data))
sprintf(logdata,"data send Successfully\n");
LogInfo(logdata);
sprintf(logdata,"data Send: %s\n",data);
LogInfo(logdata);
else if(data_len_send<sizeof(data)&& data_len_send!=0)
sprintf(logdata,"hlf data send Successfully\n");
LogInfo(logdata);
sprintf(logdata,"data Send: %s\n",data);
LogInfo(logdata);
else if(data_len_send==-1)
sprintf(logdata,"error %d \n",WSAGetLastError());
LogInfo(logdata);
closesocket(accfd);
return 0;
//child end
//while loop end
closesocket(sockfd);
printf("socket closed\n");
WinSockCleanup();
printf("cleanup done\n");
getch();
return 0;
我做了一个服务器和多客户端进程。在创建过程之后,我需要将我当前的程序 id 更改为子 id 以运行程序的其余部分。我希望子进程运行我的服务器的发送和接收部分
请帮我解决这个问题。
【问题讨论】:
嗨,欢迎来到 Stack Overflow!有点不清楚哪个部分给您带来了问题(因此我们可以帮助您解决哪个部分)。您能否将问题中的代码缩小到与您的问题相关的部分? (我们真的不需要阅读你所有的代码来找到重要的部分)。 另外——你能告诉我们更多关于“转移我当前的程序ID”的意思吗——你的意思是你想结束当前的程序并继续运行一个分叉的子进程吗?如果不是 - 请为您的问题添加更多解释(不在 cmets 中)。如果您让我们了解您在尝试运行上述代码时发生了什么,这也会有所帮助。你有错误吗?您也可以将它们放在问题中吗?否则你看到了什么,你期望看到什么?通过给我们更多的帮助来帮助我们更好地帮助您:) 我当前的代码由父级运行。创建子进程后,我希望子进程运行其余代码。不是父母。我想让你告诉我如何将我当前的进程 ID 从父进程更改为子进程 所以这是一个准确的解释:“我希望它分叉,孩子继续,父母停止”? 当您在 c 编程参考中查找“fork”时发生了什么? ;) 【参考方案1】:进程启动时会为进程分配一个 id。没有办法改变它。这也意味着您不能将其转移给任何人。
发布的代码不完整。目前还不清楚它在做什么。
您似乎也将 CreateProcess
与 Unix fork
混淆了。它们具有非常不同的语义。
【讨论】:
我在那里看到的没有任何意义。您仍然无法将进程的 ID 转移到接受的连接。您可以将此连接设置为子进程的标准输入。在任何情况下,您都不应该在每次接受连接时启动server_tcp.exe
。以上是关于无法将当前进程 ID 转换为子进程 ID的主要内容,如果未能解决你的问题,请参考以下文章