Segmentation Fault with Fork()

Hello, im having a problem with my vending machine code. We are using the Fork() fonction, basicly, the main program has to call 2 others program.

The vending machine take the money and the beverage choice.

Then it call the Money Calculator wich tell how much money it will return,

Then it call the beverage giver, it will give the beverage..

The error is a Segmentation Fault, it run but i think the Fork() or the Spintf is failing.

Vending Machine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <unistd.h>
#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
  int iMontant;
  int iMonnaie;
  int choix;
  char *schoix;
  char *ichoix;

  while(iMontant <= 80)
  {
    cout<<"Entrer une piece de monnaie";
    cin>> iMonnaie;
    iMontant += iMonnaie;
  }
  cout<<"1) Boisson 1"<<endl;
  cout<<"2) Boisson 2"<<endl;
  cout<<"3) Boisson 3"<<endl;
  cout<<"4) Boisson 4"<<endl;
  cout<<"Votre Choix : ";
  cin>>choix;

   // creation d'un nouveau processus
   int code = fork();

   // code execute par l'enfant
   sprintf(ichoix, "%d", iMontant);
   if( code == 0 )
   {
      cout << "Lancement de Calcul par le processus " << getpid() << endl;
      execl( "/home/etudiant/Documents/Sythese/Calcul", "Calcul",iMontant, NULL );
   }

    int code2 = fork(); //Boisson

   // code execute par l'enfant
   sprintf(schoix, "%d", choix);
   if( code2 == 0 )
   {
      cout << "Lancement de Tuxpaint par le processus " << getpid() << endl;
      execl( "/home/etudiant/Documents/Sythese/Fabrication", "Fabrication",schoix, NULL );
   }



   return 0;
}


Money return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include<iostream>
#include<stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
    bool cling = false;
    int MontantRestant = atoi(argv[1]) - 80;
  while(MontantRestant > 0)
  {
    if( MontantRestant >= 100)
    {
      MontantRestant -= 100;
      sleep(1);
      if(cling)
      {
        cling = false;
      }
      else
      {
        cling = true;
      }

      if(cling)
      {
        cout<< "Cling"<<endl;
      }
      else
      {
        cout <<"clang" << endl;
      }

    }
    else if(MontantRestant >= 25)
      {
        MontantRestant -= 25;
        sleep(1);

        if(cling)
        {
          cling = false;
        }
        else
        {
          cling = true;
        }

        if(cling)
        {
          cout<< "Cling"<<endl;
        }
        else
        {
          cout <<"clang" << endl;
        }
        break;
      }
    else if(MontantRestant >= 10)
	{
        MontantRestant -= 10;
        sleep(1);

        if(cling)
        {
          cling = false;
        }
        else
        {
          cling = true;
        }

        if(cling)
        {
          cout<< "Cling"<<endl;
        }
        else
        {
          cout <<"clang" << endl;
        }
        break;
      }
    else if(MontantRestant >= 5)
    {
        MontantRestant -= 5;
        sleep(1);

        if(cling)
        {
          cling = false;
        }
        else
        {
          cling = true;
        }

        if(cling)
        {
          cout<< "Cling"<<endl;
        }
        else
        {
          cout <<"clang" << endl;
        }
        break;
    }
  }
}


Giving the beverage
1
2
3
4
5
6
7
8
9
10
11


#include<iostream>
#include<stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
   sleep(3);
   cout <<"La boisson est "<<endl;
}
Hi Vincent,

I think you are correct about the sprintf. You are attempting to write to 'schoix', which is a char * . But for now, it's completely unitialized. What is this pointer pointing TO? There's no memory available for sprintf to write to. Add schoix = new char [30]; before your sprintf, and that section will work :) note you have to eventually de-allocate, and you should account for buffer overflow, etc.

-Rollie
Thanks!
Topic archived. No new replies allowed.