fork() & exec1 problem

fork() & exec1 problem

Post by Pete » Mon, 16 Jun 2003 13:38:29



The following is a fragment of a program handling locks. The problem I'm
having is in case 1: , the program checks the array and compares the array
LocalLocks[] for a 0 or 1 indicating Locked or Unlocked then printf the
result.
"the problem"
 The program forks to run a simple file "included at end of this" Master.c
and return a result. I get an error message returned by the switch/case exec
failed.
    Can someone please help as I am new to forks/exec.
    Thankyou

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

#define MAX 5
#define TRUE  1
#define FALSE 0

int LocalLocks[MAX];

/*------ProtoTypes-------*/
int TryLock(int);
void UnLock(int);
void Lock(int);
void instructions (void);

/*-------main function---------*/

int main()
{
 int LockNo, condition;
 int choice;

 pid_t pid;

 LocalLocks[0]=0;

while(1)
{
instructions();
scanf("%d", &choice);
switch (choice)
{
case 1: printf("Type lock number to try: Must be between 1 & 5\n ");
        fflush(stdout);
  scanf("%d", &LockNo);
  condition = TryLock(LockNo);
  if (condition==1)
  {
   printf("Lock %d is currently being used\n", LockNo);
  }
  else
  {
   printf("Local Lock %d is available. Checking Master Locks\n", LockNo);
   switch(pid = fork()) {
   case -1:
       printf("fork failed");
       break;
   case  0:
       /*child calls exec*/
       execl("System_prog1/Master.c", "Master", (char *)0);
       printf("exec failed\n");
       break;
   default:
       /*parent uses wait to suspend execution
        *until child finishes
        */
       wait((int *)0);
       printf("Master is completed\n");
       exit(0);
   }
  }
        break;

/*--------------TryLock Function-------------------*/

int TryLock(int x)
{
 if (LocalLocks[x - 1]== 1) {
  return(1);
 }
 if (LocalLocks[x - 1]== 0) {
  return(0);
 }

Quote:}

/*------Master.c---------*/

#include<stdio.h>
#include<stdlib.h>

#define MAX 5

int MasterLock[MAX];

int main() {

MasterLock[0] = 0;

if (MasterLock[0] = 1)
{
 printf("Lock is currently being used\n");
 return 1;

Quote:}

else
{
 printf("Lock is free\n");
 return 0;
Quote:}
}

 
 
 

fork() & exec1 problem

Post by Marc Rochkin » Mon, 16 Jun 2003 14:26:39



> The following is a fragment of a program handling locks. The problem I'm
> having is in case 1: , the program checks the array and compares the
> array
> LocalLocks[] for a 0 or 1 indicating Locked or Unlocked then printf the
> result.
> "the problem"
> The program forks to run a simple file "included at end of this" Master.c
> and return a result. I get an error message returned by the switch/case
> exec
> failed.
> Can someone please help as I am new to forks/exec.
> Thankyou

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

> #define MAX 5
> #define TRUE  1
> #define FALSE 0

> int LocalLocks[MAX];

> /*------ProtoTypes-------*/
> int TryLock(int);
> void UnLock(int);
> void Lock(int);
> void instructions (void);

> /*-------main function---------*/

> int main()
> {
> int LockNo, condition;
> int choice;

> pid_t pid;

> LocalLocks[0]=0;

> while(1)
> {
> instructions();
> scanf("%d", &choice);
> switch (choice)
> {
> case 1: printf("Type lock number to try: Must be between 1 & 5\n ");
> fflush(stdout);
> scanf("%d", &LockNo);
> condition = TryLock(LockNo);
> if (condition==1)
> {
> printf("Lock %d is currently being used\n", LockNo);
> }
> else
> {
> printf("Local Lock %d is available. Checking Master Locks\n", LockNo);
> switch(pid = fork()) {
> case -1:
> printf("fork failed");
> break;
> case  0:
> /*child calls exec*/
> execl("System_prog1/Master.c", "Master", (char *)0);
> printf("exec failed\n");
> break;
> default:
> /*parent uses wait to suspend execution
> *until child finishes
> */
> wait((int *)0);
> printf("Master is completed\n");
> exit(0);
> }
> }
> break;

> /*--------------TryLock Function-------------------*/

> int TryLock(int x)
> {
> if (LocalLocks[x - 1]== 1) {
> return(1);
> }
> if (LocalLocks[x - 1]== 0) {
> return(0);
> }
> }

> /*------Master.c---------*/

> #include<stdio.h>
> #include<stdlib.h>

> #define MAX 5

> int MasterLock[MAX];

> int main() {

> MasterLock[0] = 0;

> if (MasterLock[0] = 1)
> {
> printf("Lock is currently being used\n");
> return 1;
> }
> else
> {
> printf("Lock is free\n");
> return 0;
> }
> }

Rather than figure out what's wrong, let me instead give some general
advice that can help you with all UNIX programs you write -- not just this
one:

1. Check the error return from every system call.
2. Most report the specific cause of the error return, usually by setting a
global named errno. There are also functions (e.g., strerror) to display
the error code as an informative string.

If you rework your program such that it follows these rules, it will
probably tell you what's wrong. (Perhaps it's the argument to execl, but
why not let UNIX tell you that?)

There is no other way to program UNIX systems!

--Marc

 
 
 

fork() & exec1 problem

Post by Jens.Toerr.. » Mon, 16 Jun 2003 23:42:53



> The following is a fragment of a program handling locks. The problem I'm
> having is in case 1: , the program checks the array and compares the array
> LocalLocks[] for a 0 or 1 indicating Locked or Unlocked then printf the
> result.
> "the problem"
>  The program forks to run a simple file "included at end of this" Master.c
> and return a result. I get an error message returned by the switch/case exec
> failed.
>     Can someone please help as I am new to forks/exec.
>     Thankyou
> #include<stdio.h>
> #include<stdlib.h>
> #include<sys/types.h>
> #include<unistd.h>
> #define MAX 5
> #define TRUE  1
> #define FALSE 0
> int LocalLocks[MAX];
> /*------ProtoTypes-------*/
> int TryLock(int);
> void UnLock(int);
> void Lock(int);
> void instructions (void);
> /*-------main function---------*/
> int main()
> {
>  int LockNo, condition;
>  int choice;
>  pid_t pid;
>  LocalLocks[0]=0;

What's this initialization meant ot be good for? And if it is good for
something why only the first element of the array?

Quote:> while(1)
> {
> instructions();
> scanf("%d", &choice);
> switch (choice)
> {
> case 1: printf("Type lock number to try: Must be between 1 & 5\n ");
>         fflush(stdout);
>   scanf("%d", &LockNo);

You should check the value you got from the user before blindly
using it in TryLock() as an index in the LocalLocks array.

Quote:>   condition = TryLock(LockNo);
>   if (condition==1)
>   {
>    printf("Lock %d is currently being used\n", LockNo);
>   }
>   else
>   {
>    printf("Local Lock %d is available. Checking Master Locks\n", LockNo);

Sorry, but you're not checking any "locks" at all. You just invoke some
useless program (see below) and then wait until it finishes.

Quote:>    switch(pid = fork()) {
>    case -1:
>        printf("fork failed");
>        break;
>    case  0:
>        /*child calls exec*/
>        execl("System_prog1/Master.c", "Master", (char *)0);

You want to execute the compiled program Master, not the C source code
Master.c, don't you?

Quote:>        printf("exec failed\n");
>        break;

Why do you want to continue with the while loop when the execl() call
failed?

- Show quoted text -

Quote:>    default:
>        /*parent uses wait to suspend execution
>         *until child finishes
>         */
>        wait((int *)0);
>        printf("Master is completed\n");
>        exit(0);
>    }
>   }
>         break;
> /*--------------TryLock Function-------------------*/
> int TryLock(int x)
> {
>  if (LocalLocks[x - 1]== 1) {
>   return(1);
>  }
>  if (LocalLocks[x - 1]== 0) {
>   return(0);
>  }
> }

You could write this much shorter (and cleaner, since now also the case
that LocalLocks[ x - 1 ] is neither 0 nor 1 is handled) as e.g.

int TryLock( x )
{
    return LocalLocks[ x - 1 ] > 0;

Quote:}
> /*------Master.c---------*/
> #include<stdio.h>
> #include<stdlib.h>
> #define MAX 5
> int MasterLock[MAX];
> int main() {
> MasterLock[0] = 0;
> if (MasterLock[0] = 1)

I guess you want

 if (MasterLock[0] == 1)

here, i.e. a comparison, otherwise you're just assigning 1 to MasterLock[0].

Quote:> {
>  printf("Lock is currently being used\n");
>  return 1;
> }
> else
> {
>  printf("Lock is free\n");
>  return 0;
> }
> }

What's that program supposed to do? You initialize MasterLock[0] to 0
and then test it, assuming that you really meant a comparison above.
You could shorten the program to

int man( void )
{
    printf("Lock is free\n");
    return 0;

Quote:}

to get exactly the same effect. Or do you hope that MasterLock[0] somehow
magically will get changed between the initialization and the test?

                                       Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

fork() & exec1 problem

Post by Fletcher Glen » Wed, 18 Jun 2003 00:43:12



> The following is a fragment of a program handling locks. The problem I'm
> having is in case 1: , the program checks the array and compares the array
> LocalLocks[] for a 0 or 1 indicating Locked or Unlocked then printf the
> result.
> "the problem"
>  The program forks to run a simple file "included at end of this" Master.c
> and return a result. I get an error message returned by the switch/case exec
> failed.
>     Can someone please help as I am new to forks/exec.
>     Thankyou

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

> #define MAX 5
> #define TRUE  1
> #define FALSE 0

> int LocalLocks[MAX];

> /*------ProtoTypes-------*/
> int TryLock(int);
> void UnLock(int);
> void Lock(int);
> void instructions (void);

> /*-------main function---------*/

> int main()
> {
>  int LockNo, condition;
>  int choice;

>  pid_t pid;

>  LocalLocks[0]=0;

> while(1)
> {
> instructions();
> scanf("%d", &choice);
> switch (choice)
> {
> case 1: printf("Type lock number to try: Must be between 1 & 5\n ");
>         fflush(stdout);
>   scanf("%d", &LockNo);
>   condition = TryLock(LockNo);
>   if (condition==1)
>   {
>    printf("Lock %d is currently being used\n", LockNo);
>   }
>   else
>   {
>    printf("Local Lock %d is available. Checking Master Locks\n", LockNo);
>    switch(pid = fork()) {
>    case -1:
>        printf("fork failed");
>        break;
>    case  0:
>        /*child calls exec*/
>        execl("System_prog1/Master.c", "Master", (char *)0);
>        printf("exec failed\n");
>        break;
>    default:
>        /*parent uses wait to suspend execution
>         *until child finishes
>         */
>        wait((int *)0);
>        printf("Master is completed\n");
>        exit(0);
>    }
>   }
>         break;

> /*--------------TryLock Function-------------------*/

> int TryLock(int x)
> {
>  if (LocalLocks[x - 1]== 1) {
>   return(1);
>  }
>  if (LocalLocks[x - 1]== 0) {
>   return(0);
>  }
> }

> /*------Master.c---------*/

> #include<stdio.h>
> #include<stdlib.h>

> #define MAX 5

> int MasterLock[MAX];

> int main() {

> MasterLock[0] = 0;

> if (MasterLock[0] = 1)
> {
>  printf("Lock is currently being used\n");
>  return 1;
> }
> else
> {
>  printf("Lock is free\n");
>  return 0;
> }
> }

The thing that is most obvious to me is that you
are attempting to exec() Master.c which is a source
file and not an executable.

--
                Fletcher Glenn

 
 
 

1. Concurrent, non-(blocking&&threaded&&forking) server

Anyone tried this?

Please tell me If I do this harder than it should be done:
(assume the server and client communicates through a special protocol,
maybe some sort of authorization)
On every connect, you put the new clientfd in som client-struct and
add it to a linked list.
For everything that happens you have states, like STATE_GETPASSWORD - and
because you never know if a recv(8) gets you 8 bytes you have
to have a variable too in this struct telling me ho many bytes of the
password you have gotten.

I _have_ done this, and it works, BUT, this looks much too complicated to me.
I have also done a server using fork() on connect, but if I have a big
program and many connects I guess this is not the best way :)

Any suggestions?? Thank you!!

--

/Daniel ?kerud

2. 2.4.18-mips buffer cache problem

3. comsat forking, forking and forking (3.2.0)

4. DosEmu for a CashDesk Solution

5. forking & execing & killing

6. Linux vet needs help with Slackware 3.01 setup on 386 system

7. pthreads & fork & execve

8. Kdevelop install problem

9. gnome-terminal fork problem (long & messy)

10. Problems with fork() & for(;;)

11. &&&&----Looking for a unix shell------&&&&&

12. ppp && PPPoE && ADSL && net && buffer(s)

13. &#27809;&#26377;&#20154;&#29992;&#20013;&#25991;&#21527;&#65311;