关于system() 调用传入参数 急 快交作业了
老师给的题目是用fork生成N个父子程序让他们连成一个环,然后环形式的发送信息,现在我把简单的client 和server 做出来了并且可以从client 发送到server,但是很迷惑不知道怎么在父子process中完成现在我试着这样做(或者高手给出更好的方法),但是又不知道在system()中如何把参数也就是<port number>传送到 simser中
int main()
{
int i=0,pid,c=1,son=1,pp=1;
pid=fork();
if(pid==0)
{
//p=8;
printf("I am son %d\n",getpid());
system("simser","67");
//son++;
}else if(pid!=0)
{
//sleep(10);
printf("I am father %d\n",getpid());
// pp++;
}
我想用system()调用另外一个已经编译的simser.exe执行文件代码如下:
int main(int argc, char *argv[])
{
int servSock;
struct sockaddr_in echoServAdr;
int clntSock;
struct sockaddr_in echoClntAdr;
unsigned int clntLen;
unsigned short echoServPort;
/*create the socket for incomming call*/
if(argc!=2)
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}
echoServPort=atoi(argv[1]);
if((servSock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
printf("Socket create error");
/*struct a local address structure*/
memset(&echoServAdr,0,sizeof(echoServAdr));
echoServAdr.sin_family = AF_INET;
echoServAdr.sin_addr.s_addr=htonl(INADDR_ANY); //any incomming interface
echoServAdr.sin_port = htons(echoServPort); //the local port
/* Bind to the local address*/
if(bind(servSock, (struct sockaddr*)&echoServAdr, sizeof(echoServAdr))<0) //bind the specified information to the specified socket
printf("Bind is failed might be the port is bind already");
/*mark the socket so it will litsen for incoming connections*/
if(listen(servSock,MAXPENDING)<0) // the limitqueque
printf("Error to listen the port");
for(;;)
{
/*set the size of in-out parameter*/
clntLen=sizeof(echoClntAdr);
/*wait for the client to connect*/
if((clntSock=accept(servSock, (struct sockaddr*)&echoClntAdr, &clntLen))<0) // it will return a new socket that is connected to the client
printf("Error to accept");
/*Now is connect to the client*/
printf("Hanlding client %s\n",inet_ntoa(echoClntAdr.sin_addr));
HandleTCPclient(clntSock);
}
}
也就是一个简单的server code
请赐教
谢谢
[解决办法]
The system function passes command to the command interpreter, which executes the string as an operating-system command. system refers to the COMSPEC and PATH environment variables that locate the command-interpreter file (the file named CMD.EXE in Windows 2000 and later). If command is NULL, the function simply checks to see whether the command interpreter exists.
You must explicitly flush (using fflush or _flushall) or close any stream before calling system.
_wsystem is a wide-character version of system; the command argument to _wsystem is a wide-character string. These functions behave identically otherwise.
TCHAR.H routine
_UNICODE & _MBCS not defined
_MBCS defined
_UNICODE defined
_tsystem
system
system
_wsystem
Requirements
Routine
Required header
system
<process.h> or <stdlib.h>
_wsystem
<process.h> or <stdlib.h> or <wchar.h>
For additional compatibility information, see Compatibility in the Introduction.
Example
This program uses system to TYPE a text file.
Copy Code
// crt_system.c
#include <process.h>
int main( void )
{
system( "type crt_system.txt" );
}
Input: crt_system.txt
Copy Code
Line one.
Line two.
Output
Line one.
Line two.
[解决办法]
system("指令");
system的本质是fork一个子进程来接收你参数里输入的指令.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
linux下是这样得:
system("./test");
创建一个子进程运行test