0 Replies - 178 Views - Last Post: 20 March 2013 - 02:25 PM Rate Topic: -----

#1 lokyblue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-March 10

fork() to make more than 2 process and seperate their jobs

Posted 20 March 2013 - 02:25 PM

Hello

i m looking for answer to a just simple fork question what i am trying to achive is ,that ,when i compile this program below and
hit the enter button to run it like



./exe " findx me "


in ubuntu shell i want this program to find all "me" and all "findx" words in a txt file but !!! i wan to create a new child process for every word in this case =>>>


./exe " findx me "


i want main to do nothing and create 2 child process and those one of them look for "findx" and other one look for "me" words

but i couldnt make it work right i appriciate for all help thanks so much here is code



#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<sys/types.h>

#define LINE_LENGTH 180


int makeargv(const char *s,const char *delimiters,char ***argvp);

void freemakeargv(char **argv);

int counttokens(char **myargv,FILE *openedfile,int whichdelimiter);

int main(int argc , char *argv[] )
{
	char delim[]=" \t";
	int i;
	char **myargv;
	int numtokens;
	int bla=0;
	
	pid_t childpid;
	
	
	FILE *inputfile;
	
	inputfile=fopen("example.txt","r");
	
	fprintf(stderr," argc is %d !!\n",argc);
	
	printf(" i am process and my id is %ld\n",(long)getpid);
	
	fprintf(stderr," my parents id is %ld but i m in main\n",(long)getppid);	

	if(inputfile==NULL)
	{
		fprintf(stderr,"file is not opened exits!!\n");
		return 1;
		
	}
	
	fprintf(stderr,"usage  :%s , %s , %s , %s  strings here\n",argv[0],argv[1],argv[2],argv[3]);
	
	if(argc < 2)
	{
		fprintf(stderr,"usage  :%s  strinwwwwwwg\n",argv[0]);
		return 1;
	}
	
	if((numtokens =makeargv(argv[1],delim,&myargv))==-1)
	{
		
		fprintf(stderr,"failed to construct an argumant array for %s \n",argv[1]);
		return 1;
		
	}

	printf("the argument array containsererer  %d is tokens: \n",numtokens);
	
	for(i=0;i<numtokens; i++)
	{
		
		printf("\n%d :%s \n",i,myargv[i]);
	}
	
	/*işte burayı forklaman lazım !!!*/
	
	for(i=0;i<numtokens;i++)
	{
		
		if(childpid=fork())
		{
			printf(" \ni m  %d.thchild process and my id is %ld \n",i,(long)getpid());
			
			printf(" parent ******i m child process and my id is %ld \n",(long)getppid());
		
			bla=counttokens(myargv,inputfile,i);
	
			printf("\n %d tokens total\n",bla);
			
			
			
			break ;
		}else if(childpid== -1)
		{
			
			perror("\n**failed to fork \n");
			return 1;
			
		}
		
		
		
	}
/*
	childpid=fork();
	if(childpid== -1)
	{
		perror("\n**failed to fork \n");
		return 1;
	}
	
	if(childpid==0)
	{
		printf(" \ni m child process and my id is %ld \n",(long)getpid());
		
		printf(" parent ******i m child process and my id is %ld \n",(long)getppid());
		
		bla=counttokens(myargv,inputfile,0);
	
		printf("\n %d tokens total\n",bla);
	}
	else
	{
		printf("i m parent %ld yoo\n",(long)getpid());
		
		
	}
*/
	
	freemakeargv(myargv);
	fclose(inputfile);
	
	return 0;
}
 
 
int makeargv(const char *s,const char *delimiters,char ***argvp)
{
	
	int error;
	int i;
	int numtokens;
	const char *snew;
	char *t;
	
	if((s == NULL) || (delimiters==NULL) || (argvp==NULL))
	{
		errno=EINVAL;
		return -1;
		
	}
	
	*argvp=NULL;
	
	snew= s + strspn(s,delimiters);
	
	if((t=malloc(strlen(snew)+1)) ==NULL )
		return -1;
	
	strcpy(t,snew);
	
	numtokens=0;
	
	if(strtok(t,delimiters)!= NULL)
		for(numtokens=1;strtok(NULL,delimiters) != NULL ; numtokens++);
	
	if((*argvp=malloc((numtokens+1)*sizeof(char *)))==NULL)
	{
		error =errno;
		free(t);
		errno=error;
		return -1;
	}
	
	if(numtokens==0)
		free(t);
	else
	{
		strcpy(t,snew);
		**argvp=strtok(t,delimiters);
		for(i=1;i<numtokens ;i++)
			*((*argvp)+i)=strtok(NULL,delimiters);
	}
	
	*((*argvp) +numtokens)=NULL;
	
	
	return numtokens;
	
	
	}



void freemakeargv(char **argv)
{
	
	
	if(argv == NULL)
		return;
		
	if(*argv != NULL)
		free(*argv);
		
	free(argv);
	
	
	
	}

int counttokens(char **myargv,FILE *openedfile,int whichdelimiter)
{
	char *delimiter;
	int lengthOfDelimeter=0;
	int error;
	char *beginingFileDelimiter;
	char *endingFileDelimiter;
	
	
	/*ikinci bölümün variableları*/
	
	char *extraline;
	char line[LINE_LENGTH];
	char *status;
	int i=0;
	int founder=12;
	int counter=0;
	int linelength=0;
	
	
	
	lengthOfDelimeter=strlen(myargv[whichdelimiter]);
	
	//makes new delimeter string!
	
	if((delimiter=malloc((lengthOfDelimeter+3)*sizeof(char *)))==NULL)
	{
		error =errno;
		free(delimiter);
		errno=error;
		return -1;
	}
	
	
	
		
	/*for in middle begin and end of file searching delimiter*/
	
	strncpy(&delimiter[0]," ",1);
	strncpy(&delimiter[1],myargv[whichdelimiter],lengthOfDelimeter);
	strncpy(&delimiter[lengthOfDelimeter+1]," ",1);
	strncpy(&delimiter[lengthOfDelimeter+2],"\0",1);
	
	
	
	printf("\n%s-%d\n",delimiter,(int)strlen(delimiter));
	
	/*delimiter belli elimizde dosyada var simdi dosyadan delimiterleri bulup 
	 * main e geri götürmeliyiz*/
	
	
	/*ilk satırın okunması*/
	
	for(status=fgets(line,LINE_LENGTH,openedfile);
		status!=0;
		status=fgets(line,LINE_LENGTH,openedfile))
	{
	
		if(status == NULL)
		{
			fprintf(stderr,"theres  not anything in file !! exits \n");
			return 1;
		}/*end if*/
		else
		{
			if(line[strlen(line)-1] == '\n')
				line[strlen(line)-1] ='\0';
			
		
			founder=strncmp(line,&delimiter[1],strlen(delimiter)-1); //it can begin whitout space
		
			linelength=strlen(line);
		
			if(founder==0)
			{
				printf("%d.th one found  \n",++counter);
			/*end inner if*/
			}
			for(i=strlen(delimiter)-2;i<linelength;)
			{
				founder=12; /*junk deger*/
			
				founder=strncmp(&line[i],delimiter,strlen(delimiter)); 
			
				if(founder==0)
				{
				
					printf("%d.th one found  \n",++counter);
					i+=strlen(delimiter) -1;	/*bulduysa diğer sefere buradan başlayacak*/
				
				}/*end inner if*/
				else
				{
					i++; /*bulamadıysa gelecek sefere hemen bir sonraki karakterden baslacyacak*/	
				}/*end inner else*/
			
			}/*end for*/
			
			/*may end with bili word*/
			if(linelength>=(strlen(delimiter) - 2))
			{
				founder =12;
				/*
				founder=strncmp(&line[linelength-(strlen(delimiter)-1)],&delimiter[0],strlen(delimiter)-1); //it can begin whitout space
				
				if(founder==0)
				{
					printf("%d.th one found  \n",++counter);
				
				}
				founder=12;
				*/
				
				founder=strncmp(&line[linelength-(strlen(delimiter)-2)],&delimiter[1],strlen(delimiter)-2); //it can begin whitout space
				
				if(founder==0)
				{
					printf("%d.th one found  \n",++counter);
				
				}
				
			
			}
			
			
		
		}/*end else*/
	
	} /*end outer for*/
	
	
	
	return counter;
	
}











by the way rest of code is working great i only trying to solve fork part

Is This A Good Question/Topic? 0
  • +

Page 1 of 1