5 Replies - 2225 Views - Last Post: 27 February 2010 - 10:05 AM Rate Topic: -----

#1 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Segmentation fault (core dumped) error?

Posted 27 February 2010 - 08:59 AM

Good morning!

I am working with the following:

#include <stdio.h>
int main()
{
int pid, i;
pid = vfork();
switch(pid)
{
case -1: /* an error occurred */
printf("Fork error");
break;
case 0: /* this code is executed by the child process */
for(i=1; i<10000; i++)
printf("Child process, iteration: %d\n", i);
break;
default: /* this code is executed by the parent process */
for(i=1; i<10000; i++)
printf("Parent process, iteration: %d\n", i);
}
}



The idea is to have the child process finish all of its work and then the parent process starts printing. When I upload the file to the server, compile and run it, it only seems to run the child process and then errors out. I receive the following:

...
child process, iteration: 9995
child process, iteration: 9996
child process, iteration: 9997
child process, iteration: 9998
child process, iteration: 9999
Segmentation fault (core dumped)

So, my question is why does the parent process not start after the child process has finished? I'm probably missing something very obvious here...

Thank you very much in advance for any and all comments/suggestions.

Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation fault (core dumped) error?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: Segmentation fault (core dumped) error?

Posted 27 February 2010 - 09:20 AM

Core dump? Investigate it:

gdb exename corefilename


Assuming, of course, you're using the GNU toolchain.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: Segmentation fault (core dumped) error?

Posted 27 February 2010 - 09:29 AM

Ah, I think I found the problem. From here:

Quote

You must be very careful not to allow the child process created with vfork to modify any global data or even local variables shared with the parent. Furthermore, the child process cannot return from (or do a long jump out of) the function that called vfork! This would leave the parent process's control information very confused. If in doubt, use fork instead.

Emphasis mine.

When I changed the case 0: in your switch to exit(0) instead of break, no seg fault.
Was This Post Helpful? 0
  • +
  • -

#4 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Re: Segmentation fault (core dumped) error?

Posted 27 February 2010 - 09:29 AM

How would I determine the corefilename?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: Segmentation fault (core dumped) error?

Posted 27 February 2010 - 09:32 AM

Well, read my latest post for an answer. I actually ran it in the debugger and it wasn't helpful for exactly the reasons stated in the linked document.

But for future reference usually there's a core file (named core) in the directory where you're executing, but your environment can make that not the case...sometimes it will be core.PID (PID = process ID, i.e., core.23456), or off in some other directory.
Was This Post Helpful? 0
  • +
  • -

#6 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Re: Segmentation fault (core dumped) error?

Posted 27 February 2010 - 10:05 AM

Thanks for your guidance this morning. It works like a charm. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1