Passing a value in for()

Passing a value in for()

Post by Pete » Mon, 16 Jun 2003 18:32:02



I need to pass a value ie: user selected Lockno which is scanfed in case 1:
within this case I have a fork() to a program "Master" code at end. I am
trying to run Master using the scanfed value Lockno, without success. Please
can someone show me how to pass a value in the fork call if it is possible.
Thankyou

/*Code with fork()*/
int main()
{
 int LockNo, condition;
 int choice;

 pid_t pid;

 LocalLocks[0]=0;
 LocalLocks[1]=0;
 LocalLocks[2]=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("Master","Lockno", "-1", (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;
   }

        break;
        }

/*Code of Master*/

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

#define MAX 5

int MasterLock[MAX];

int main() {
int Lockno;

MasterLock[0] = 0;
MasterLock[1] = 0;

scanf("%d", &Lockno);

if (MasterLock[Lockno-1] = 1)
{
 printf("Master Lock is currently being used\n");
 /*return 1;*/

Quote:}

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

 
 
 

Passing a value in for()

Post by Jens.Toerr.. » Tue, 17 Jun 2003 20:18:17



> I need to pass a value ie: user selected Lockno which is scanfed in case 1:
> within this case I have a fork() to a program "Master" code at end. I am
> trying to run Master using the scanfed value Lockno, without success. Please
> can someone show me how to pass a value in the fork call if it is possible.
> Thankyou
> /*Code with fork()*/
> int main()
> {
>  int LockNo, condition;
>  int choice;
>  pid_t pid;
>  LocalLocks[0]=0;
>  LocalLocks[1]=0;
>  LocalLocks[2]=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);

fflush() isn't needed here. Your string ends with a '\n' which auto-
matically makes the printf() call flush the output buffer.

Quote:>   scanf("%d", &LockNo);

You should check if you really got an integer by testing the return value
of scanf(), if it isn't 1 the user didn't input an integer. And then you
also should check if it's in the range of values you expect, i.e. between
1 and 5 before using it.

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);
>    switch(pid = fork()) {
>    case -1:
>        printf("fork failed");
>        break;
>    case  0:
>        /*child calls exec*/
>        execl("Master","Lockno", "-1", (char *)0);

I have the impression that you want to pass the value of the variable
LockNo to Master. If that's the case you need to do this differently.
You need an additional char array, lets call it 'ln' and define it
somehwere at the start of the function e.g. as

char ln[ 50 ];

Now you do here:

        snprintf( ln, 50, "%d", LockNo );
                execl( "Master", "Master", ln, NULL );

The first and the second argument to execl() are the name of the program
to be run. The third is now the string with the value of LockNo. Instead
of '( char * ) 0' I prefer to use a simple 'NULL', which is a bit
simpler to read. All the arguments starting with the second will appear
as the argv[] array in the exec-ed program. How you now get at the string
with the value of LockNo you can see from my version of Master below.

Quote:>        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;

No need for a break after the exit().

Quote:>    }
>         break;
>         }
> /*Code of Master*/
> #include<stdio.h>
> #include<stdlib.h>
> #define MAX 5
> int MasterLock[MAX];
> int main() {
> int Lockno;
> MasterLock[0] = 0;
> MasterLock[1] = 0;
> scanf("%d", &Lockno);
> if (MasterLock[Lockno-1] = 1)

You mean "==" here for a comparison, don't you?

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

Make this

int main( int argc, char *argv[ ] )
{
        int LockNo;

    if ( argc < 2 )
    {
        fprintf( stderr, "Missing argument.\n" );
                return EXIT_FAILURE;
        }

        if ( sscanf( argv[ 1 ], "%d", &LockNo ) != 1 )
        {
                fprintf( stderr, "Argument isn't an integer.\n" );
                return EXIT_FAILURE;
        }

        if ( LockNo < 1 || LockNo > MAX )
        {
                fprintf( stderr, "Argument is out of range.\n" );
                return EXIT_FAILURE;
        }

        if ( MasterLock[ Lockno - 1 ] == 1 )
                printf( "Master Lock is currently being used\n" );
        else
                printf( "Master Lock is free\n" );

        return EXIT_SUCCESS;

Quote:}

Of course, this will not do anything useful yet because MasterLock is
an array local to your Master program. But I guess there's still some
code missing that you use to initialize MasterLock with, e.g. from the
contents of a file and that you don't show here...

                                  Regards, Jens
--
      _  _____  _____

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

 
 
 

Passing a value in for()

Post by Barry Margoli » Wed, 18 Jun 2003 04:24:55





>> case 1: printf("Type lock number to try: Must be between 1 & 5\n ");
>>         fflush(stdout);

>fflush() isn't needed here. Your string ends with a '\n' which auto-
>matically makes the printf() call flush the output buffer.

Not if stdout is redirected to a file or pipe.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Passing a value in for()

Post by Geoff Clar » Wed, 18 Jun 2003 21:50:41



>            execl( "Master", "Master", ln, NULL );
>The first and the second argument to execl() are the name of the program
>to be run. The third is now the string with the value of LockNo. Instead
>of '( char * ) 0' I prefer to use a simple 'NULL', which is a bit
>simpler to read.

You are, of course, free to choose to make your code simpler to read
at the expense of portability, but please don't advise others to do
the same.  I'm sure most other people would prefer their code to be
fully portable at the expense of a little readability, and use either
(char *)NULL or (char *)0 as the last argument in execl() calls.

--

 
 
 

1. How to enter values during pkgadd installation and pass those values to postinstall process?

All,

Can I pass inputs into the command so that they will be processed by
postinstall? For example, can i do the following:

"pkgadd -d software_a.pkg STRING1 STRING2" ?

STRING1 and STRING2 are just string variables.

I would like to be able to doing something in the postinstall section of the
spec file such as:

"echo $STRING1"
"echo $STRING2"

Of course, I will do more than just echo, but having the abilities to do
echo meaning I can use those strings however I want during the postinstall
process. Essentially, STRING1 can be a servername, and the postinstall
section of the spec file needs to process this servername or start processes
with this string.

Thanks

Barry

2. Dead keys in Linux/XFree86 3.2

3. How do you pass a value from a shell script into a sqlplus script?

4. mkinitrd: "aic7xxx.o: binary operator expected"

5. pass values back to parent sell

6. solaris 2.5.1: kadb bug?

7. Passing values generated in awk/nawk back to script

8. killing processes at logout

9. passing value..

10. Passing Value to Linux Script from CGI

11. pass back return value to C program from Bourne shell?

12. Reading a file and passing it as a value to a script.

13. Passing system command value's