i am tryng to check the pid for the parent and child process, but whenever i run the program what i get is the same pid value for both of them.
from what i know getpid gets the pid of current process. Am i wrong? same valur of pid for both processes
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main (void){
pid_t pid= fork();
switch (pid){
case -1:
perror("fork");
exit(1);
break;
case 0 :
printf("Child Process - my pid: %d, my parent's pid: %d\n", (int)getpid, (int)getppid);
break;
default :
wait();
printf("parent process - my pid: %d, my parent's pid: %d, my child's pid: %d\n", (int)getpid, (int)getppid, (int)pid);
break;}
printf("End of fork\n");
return 0;
}
You haven't called the getpid or getppid functions, you've only cast them to integers. You need to add parentheses.
printf("parent process - my pid: %d, my parent's pid: %d, my child's pid: %d\n", getpid(), getppid(), pid);
Notice the bad values being printed are well outside the PID range, which is typically 32k. Your parent process is printing the correct child PID because that value is an integer when it's handed to you.
Related
I am learning mutex usage between process. I have a question w.r.t this.
Since each process has its own address space, the mutex defined by one process cannot be seen by another. On googling, I came across usage of pthread_mutex_setpshared() to get this done. I am attaching my code below. But looks like even now, the mutex is not shared between processes. How is this to be modified.
I know this can be achieved using named semaphore. But want to know the usage using mutex.
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>
int main()
{
pid_t pid;
int ch = 0;
//sem_t *s1;
//s1 = sem_open("/sem", O_CREAT, 0666, 0);
pthread_mutex_t m1;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&m1, &attr);
pid = fork();
if(pid == 0)
{
pthread_mutex_lock(&m1);
printf("\nChild process");
printf("\nPress 1 to unlock from child:");
scanf("%d", &ch);
if(ch == 1)
{
pthread_mutex_unlock(&m1);
}
//sem_post(s1);
}
else if (pid > 0)
{
pthread_mutex_lock(&m1);
//sem_wait(s1);
printf("\nParent process");
printf("\nPress 2 to unlock from parent:");
scanf("%d", &ch);
if(ch == 2)
{
pthread_mutex_unlock(&m1);
}
}
else
{
printf("\nError");
}
}
As well as setting the PTHREAD_PROCESS_SHARED attribute, you need to arrange to have the mutex itself (your m1 variable) stored in shared memory, accessible by both processes. A duplicate, as created by your fork(), does not work.
If you use mmap() to create an anonymous mapping with the MAP_SHARED flag, and allocate the mutex within that, then it will be shared between the processes after the fork() and will work.
I wrote a Project.exe that shows Hello World. Write a main process to create a child process of Project.exe. However, when I put Projetc.exe on C:/Windows/System32/, I can't create the child process of Project.exe correctly, but it can be created normally when I put it in other directories.
The main program is as follows:
#include <Windows.h>
#include <string.h>
#include <stdio.h>
int main() {
STARTUPINFO si;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
wchar_t *p = (wchar_t*)TEXT("C:/Windows/System32/Project1.exe");
CreateProcess(p, 0, 0, 0, 0, 0, 0, 0, &si, &pi);
DWORD CurId = GetCurrentProcessId(); //Get the ID of the current process
DWORD Pid = pi.dwProcessId; //ID of the created process
DWORD Tid = pi.dwThreadId; //The main thread ID of the created child process
printf("the ID of the current process: %d\nID of the created process: %d\nThe main thread ID of the created child process %d\n", CurId, Pid, Tid);
WaitForSingleObject(pi.hProcess, -1);
}
The result of the operation is:
If I put Project.exe under E:/, the result is
What is the reason for this, how do I need to change it to run successfully under /System32/?
The general answer is, if you want to know why a call isn't doing what you expect, you should first look at the return status to find out whether the call worked.
The documentation for CreateProcess tells you it returns non-zero for success, zero for failure. You are not looking at the return value, so your code has no knowledge of success or failure.
Furthermore, on failure, GetLastError() will return a reason for failure. That might tell you what's going on.
In short, you need "if return value is zero, print value of GetLastError() and exit".
I want see what will getppid() is returned function when child process will execute. I entered sleep(10) to complete parent process. Actually I should obtain "CHILD: My parent PID -- 1\n", because this child process should have init process as parent if parent does not call wait().
But I am obtaining PPID=959. This is not init-process. This PPID corresponds to some /sbin/upstart --user command.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
int status;
pid_t pid, ch_pid;
switch(pid=fork()) {
case -1:
perror("Fork failed");
exit(1);
case 0:
sleep(10);
printf("\nCHILD: This is child process!\n");
printf("CHILD: My PID is-- %d\n", getpid());
printf("CHILD: My parent PID -- %d\n", getppid());
//sleep(5);
exit(0);
default:
//sleep(60);
/*Prohibit PP to execute*/
printf("PARENT: This is parent process!\n");
printf("PARENT: My PID -- %d\n", getpid());
printf("PARENT: My child PID %d\n",pid);
printf("PARENT: My parent PID %d\n",getppid());
exit(0);
}
return 0;
}
Don't pay attention on comments.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int i = 0;
fork();
for(;i<3; ++i)
printf("%d", i);
fork();
return 0;
}
Here is my code. I want to know how many processes do I have after executing the last fork()
Fork splits the current process into 2 processes, so you have 2 after the first fork and 4 after the second.
Edit: After the first fork() there will be two processes, both executing the following statements. The initial process and the forked process will both call fork() the second time, resulting in 4 total processes after that call. For more info check out this link: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
everyone. I've got a problem that is making me very confused. I'm just trying to print out the status received from a terminated process but it isn't working the way I thought it would. Here is the code.
int main(int argc, char *argv[])
{
printf("this process (%d)\n",(int)(getpid()));
int *status;
pid_t pid;
Signal(SIGINT, handler1);
if ((pid = fork())==0){
while(1)
;
}
kill(pid,SIGINT);
while(pid>0){
pid = waitpid(pid,status,0);
printf("status: %d\n", WEXITSTATUS(status));
printf("waitpid return: %d\n",(int)pid);
}
return 0;
}
void handler1(int sig){
printf("process (%d) has received a sigint\n",(int)(getpid()));
exit('d');
}
it has output
this process (8811)
process (8812) has received a sigint
status: 0
waitpid return: 8812
status: 0
waitpid return: -1
when I use WIFEXITED(status) it returns true. So shouldn't WEXITSTATUS return what I passed through exit and not 0?
I did not include the Signal function here.
Here's your problem
int *status;
...
pid = waitpid(pid,status,0);
printf("status: %d\n", WEXITSTATUS(status));
You're not allocating space for the status. You're just declaring a pointer (which is probably initialized to 0, or NULL when we're talking pointers), and then passing it to waitpid (which, if it receives a NULL for the status, ignores it, which prevents a segfault), and then you're passing that NULL pointer to the EXITSTATUS() macro which wants an int (but it treats it as a zero, anyway). And you get "status: 0".
What you want to do is declare your status as a regular int, and then pass its pointer to waitpid():
int status;
...
pid = waitpid(pid,&status,0);
printf("status: %d\n", WEXITSTATUS(status));
This will print out "status: 100" like you're looking for.