[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

forking processes



Hello,
Several weeks ago I reported having difficulties debugging a program that forks itself using Anjuta. However, my most recent observations show that the program never really forks itself. The fork() function always returns a positive PID and skips the child process clause. Just to make sure, I added some print statements in the child and parent processes. As I suspected, the print statement in the child process is never executed. Is it possible that there is a bug in my kernel? The program used to work on my old Redhat Linux 7.1, using kernel 2.4.8. Now, however, I'm using Debian 3.0r1 with kernel 2.4.18, and I've found some vague reports on some message boards that there is a subtle bug involving fork() in the 2.4.x kernels. I'd appreciate any help. Here's the code for my program
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define MAXQUEUE 20 //how many client requests to queue
#define PORT_NUM 32000
int main(int argc, char **argv)
{
	int listenfd; //descriptor for the listening socket
struct sockaddr_in sock; //socket structure pid_t pid; //stores the process ID
	//get a socket descriptor
listenfd = socket(AF_INET, SOCK_STREAM, 0);
	//zero-out socket structure
bzero(&sock, sizeof(sock));
	//initialze sock structure
	sock.sin_family = AF_INET;
	sock.sin_addr.s_addr = htonl(INADDR_ANY);
sock.sin_port = htons(PORT_NUM);
	//bind the port and IP address to the socket:
bind(listenfd, (struct sockaddr *) &sock, sizeof(sock)); listen(listenfd, MAXQUEUE); printf("Socket serving running and listening!\n");
	while(1) //infinite loop
	{
int connectionfd = accept(listenfd, NULL, NULL); //accept a user
              /**************************************
              *  Buggy part                         *
**************************************/ pid = fork(); //split execution
		if(pid == 0) //child process
		{
			//close listening socket
close(listenfd);
			printf("Entering child process!\n");
send(connectionfd, "Ok!", sizeof("Ok!"), 0);
			exit(0);
		}
		printf("Entering parent process!\n");
		close(connectionfd);
} }


Reply to: