Hello...

I am working on a program and I don't quite understand why my last line "Child completed" is not printed out. My idea is to output for the main process in the task, (i) the executable and its pathname used and (ii) the command line used.

I have the following codes:
Code:
int main() {
        int pid, child_pid;
	char *pid_string;
	char *buf;
	char *temp = "/proc";
	pid = fork();
	if(pid > 0) {	
		printf("Parent\n");
		wait(NULL);
	}
	
	else if(pid == 0) {
		printf("Child\n");
		child_pid = getpid();
		printf("temp1: %s", temp);
		
		sprintf(pid_string, "%d", child_pid); // Convert integer to string
		printf("pid_string %s", pid_string);
		
		strcat(temp, pid_string);
		strcat(temp, "/exe");
		printf("\ntemp3: %s\n", temp);
		
		int len = readlink(temp, buf, sizeof(buf)-1);
		
	 	if (len == -1) {
			// The call failed.
			if (errno == EINVAL) {
				// It's not a symbolic link; report that.
				fprintf (stderr, "%s is not a symbolic link\n", temp);
			}
			
			else {
				// Some other problem occurred; print the generic message.
				perror("readlink");
				return 1;
			}
			
		}
		
		else {
			// NUL-terminate the target path.
			buf[len] = '\0';
			// Print it.
			printf ("%s\n", buf);
			return 0;
		}
		
		printf("\nChild completed\n");
	}
	
	else {
		perror(" fork failed...");
		exit(1);
	}
	
	return 0;
}
My problem is, I don't get the words "Child completed" printed at the end when the child finishes. However, if I commet away the codes from sprintf to the bracket just before the printf for "Child completed", I get it printed out.

Is there something wrong with readlink, or is there already something wrong with the conversion of the integer of pid to string?

I know readlink would get me the path using readlink on the file exe in the /proc folder, exit status is from the waitpid(). What about the processes and commandlines (which i omitted for this code)? Should I readlink the cmdline in the /proc/[pid] folder as well?

Thanks a lot... =)