|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Sorry, 6 is the argument. Why apperars [num] got: [num], [num]?? Is the PID?
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Code:
[num] got: [num], [num]
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
|
|||
|
|||
|
Thanks!!
Thanks!! Now, I stand it!
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
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 |
|
|