Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How I can explain this?

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-03-2013
dakota dakota is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 6 January 2013, 7:22 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Wrench How I can explain this?

Hi friends! I'm learning UNIX and I have a small question. Working with Shell, i put the name of one executable (in c language) + one number and it says this:


Code:
$ gcc misterioso_4.c 
$ ./misterioso_4 6 

[4841] got: [4667],[2] 

I can not find an answer in the manual because I havent applied any variable.

Thanks for all!

Last edited by Scott; 01-03-2013 at 05:21 PM.. Reason: Code tags
Sponsored Links
    #2  
Old Unix and Linux 01-03-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 25 April 2015, 1:28 PM EDT
Location: India
Posts: 1,878
Thanks: 27
Thanked 538 Times in 515 Posts
On the top of my head I can think of two things.
1. Is your C program coded to handle an argument, such as 6?
2. And the compiled output of gcc is usually an object file "a.out". Unless you've renamed a.out to misterioso_4, I don't see how you mean to run the C program by typing ./misterioso_4. Please re-check.
Sponsored Links
    #3  
Old Unix and Linux 01-03-2013
dakota dakota is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 6 January 2013, 7:22 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
I think 2, code acts on a child process.
The code is this:


Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#define BUFSIZE 256
int main(int argc, char *argv[])
{
pid_t childpid;
int fd[2], status, exit_code, a, b, x;
char buf[BUFSIZE];
unsigned strsize;
exit_code = EXIT_SUCCESS;
if (argc != 2)
{
printf(“Usage: %s digito_2\n”, argv[0]);
exit_code = EXIT_FAILURE;
}
else
{
if (pipe(fd) != 0)
{
perror(“Could not create the pipe\n”);
exit_code = EXIT_FAILURE;
}
else
{
switch (childpid = fork())
{
case -1:
perror(“Could not fork\n");
exit_code = EXIT_FAILURE;
break;
case 0:
if (dup2(fd[1], STDOUT_FILENO) < 0)
{
perror(“Child: fd duplication failed\n”);
exit_code = EXIT_FAILURE;
}
else
{
close(fd[0]);
close(fd[1]);
a = (int) getpid();
b = atoi(argv[1]);
sprintf(buf, “[%d],[%d]\n”, a, a % b);
strsize = strlen(buf) + 1;
if (write(STDOUT_FILENO, buf, strsize) != strsize)
{
perror(“Child: write to pipe failed\n”);
exit_code = EXIT_FAILURE;
}
} // else
break;
default:
if (dup2(fd[0], STDIN_FILENO) < 0)
{
perror(“Parent: fd duplication failed\n”);
exit_code = EXIT_FAILURE;
}
else
{
close(fd[0]); 
close(fd[1]); 
if (x = wait(&status) != childpid)
{
perror(“Parent: unexpected error\n”);
}
else
{
if (status == 0)
{
if (read(STDIN_FILENO, buf, BUFSIZE) <= 0)
{
perror(“Parent: pipe read failed\n”);
exit_code = EXIT_FAILURE;
}
else
{
printf(“[%d] got: %s\n”, (int) getpid(), buf);
}
}
else
{
perror(“Parent: child had problems\n”);
exit_code = EXIT_FAILURE;
}
}
} // end if default
} // end switch
} // end if pipe
} //end if argc
exit(exit_code);
} // end main

    #4  
Old Unix and Linux 01-06-2013
dakota dakota is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 6 January 2013, 7:22 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, 6 is the argument. Why apperars [num] got: [num], [num]?? Is the PID?
Sponsored Links
    #5  
Old Unix and Linux 01-06-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 20 April 2015, 6:16 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 Posts

Code:
[num] got: [num], [num]

  1. [num] is the Parent PID
  2. [num] is the Child PID
  3. [num] is the result of modulo operation performed below:

Code:
sprintf(buf, "[%d],[%d]\n", a, a % b);

Then result is printed:

Code:
printf("[%d] got: %s\n", (int) getpid(), buf);

Sponsored Links
    #6  
Old Unix and Linux 01-06-2013
dakota dakota is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 6 January 2013, 7:22 PM EST
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!!

Thanks!! Now, I stand it! Unix or Linux Image
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please explain this Diddy Shell Programming and Scripting 2 02-02-2011 02:09 PM
can any one explain this example maxim42 Shell Programming and Scripting 4 05-27-2009 01:43 PM
can anyone explain this? honeym210 AIX 2 08-07-2008 12:46 PM
Please can any one explain this ${0##/} gadege Shell Programming and Scripting 2 04-01-2008 03:26 PM
if [ $? -eq 0 ] .. can someone explain this? ranjita.c Shell Programming and Scripting 5 10-03-2006 07:50 PM



All times are GMT -4. The time now is 12:49 AM.