开发者

Can I simulate multiple clients this way?

I wrote a small server program. I wanted to see how it is handling multiple requests. So I wrote the following program to simulate multiple clients.

Pseudo Code:

main()
{

    //set up all necessary data structures to connect to the server

    fork();
    fork();
    开发者_开发技巧fork();

    create_socket();
    connect()

    //more code

}

Is there a better way of doing it? What tools I can use to test multi threaded program in C(at least the basic functionality)?


You've basically created a "process-fan" with this approach, so yes, that can work, although it's not threading ... you're actually creating new processes. Therefore you will want, in order to prevent zombie child processes, to "wait" for all processes to complete in each process that has spawned a new process. You could do this with the following line at or near the end of your main() for all processes that have called fork() (i.e., include the child-processes as well since they are spawning additional processes):

while(wait(NULL) != -1 || errno == EINTR);

This will wait for all the child-processes the current process has spawned, while preventing any early returns of wait() due to your process catching a signal. When there are no remaining child-processes for the current process, then wait() will return -1 and set errno to ECHILD, thus exiting the while-loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜