C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2009, 12:39 PM   #16
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
>> Gives me: open: No such file or directory
You'll need additional open flags if you expect the file to be created for you. http://www.opengroup.org/onlinepubs/...ions/open.html

>> but it doesnt write anything to stdout.txt either
You haven't even saved the file handle returned by open(). Why would you expect anything to be written to it?

>> I'm guessing close(1) closes stdout?
Yes, 1 corresponds to standard out.
http://www.opengroup.org/onlinepubs/...ons/stdin.html

>> Why _exit(1) vs exit(1)?
http://www.opengroup.org/onlinepubs/...ons/_Exit.html

It seems to me that you've jumped into the deep end without first learning to swim...
http://www.amazon.com/Pointers-C-Ken.../dp/0673999866

gg
Codeplug is online now   Reply With Quote
Old 02-04-2009, 01:26 PM   #17
Registered User
 
Join Date: Feb 2009
Posts: 9
Code:
#include <stdio.h>
#include <string.h>

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

     char small[32], big[128];
     int i = 16;

if(strstr(argv[1], "t8")) { i = 8; }
if(strstr(argv[1], "t16")) { i = 16; }
if(strstr(argv[1], "t32")) { i = 32; }
if(strstr(argv[1], "t64")) { i = 64; }
if(strstr(argv[1], "t128")) { i = 128; }

     memset(big, 'Z', sizeof(big));

     snprintf(small, i, "%s", big);
     printf("%s\n", small);

     return 0;

}
That is the program I am running through the parent. As you can see, if the values are t64 or t128, the program crashes. I simply want to run that program 5 times with the 5 different values and be able to see the segmentation faults when they occur.

Code:
bash$ ./bvss t8
ZZZZZZZ
bash$ ./bvss t16
ZZZZZZZZZZZZZZZ
bash$ ./bvss t32
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
bash$ ./bvss t64
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Segmentation fault
bash$ ./bvss t128
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Segmentation fault
bash$

bash$ ./test
ZZZZZZZ
ZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
bash$
Code:
void run(char *bin, char *data)
{

if(fork() == 0)
{

if(execl(bin, bin, data, 0) == -1)
{

     perror("execl");
     _exit(1);

}
}

else
{

int pid, status;

     pid = wait(&status);

}
}

.....

for(i = 0; i < 5; i++)
{

     run(PATH, data);

}
See as I've explained my complete situation, I still can't get my code to work. I ask kindly for a solution.
tadams is offline   Reply With Quote
Old 02-04-2009, 01:46 PM   #18
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> snprintf(small, i, "%s", big);
Well yeah, when you lie about the size of the buffer, then it's gonna hurt you at some point.

Anyway, it will be the shell which prints segmentation fault, not the program which dies.

Having done a wait(&status), you need to read this
http://www.manpagez.com/man/2/waitpid/
and do things like
Code:
if ( WIFSIGNALED(status) ) {
  int sig = WTERMSIG(status);
  printf( "Program died due to signal %d\n", sig );
}
etc and so forth.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-04-2009, 01:50 PM   #19
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
You're lucky that bvss doesn't segfault every time you run it since small is never null-terminated.

>> Segmentation fault
That's coming from bash, not bvss. So you can either run bvss via bash or detect the segfault and printf("Segmentation fault") yourself.

gg
Codeplug is online now   Reply With Quote
Old 02-04-2009, 03:29 PM   #20
Registered User
 
Join Date: Feb 2009
Posts: 9
Thanks both of you, I finally got it to work using the signal catching solution.

Couple short questions:

1) snprintf() null terminates for us, so I don't need to do a snprintf(blah, sizeof(blah)-1 just sizeof(blah), correct?

2) With memset(blah, 'Z', sizeof(blah)), could I and would I need to null terminate blah like blah[SIZE] = '\0' or blah[SIZE-1] = '\0' or ?

Thanks again for you help
tadams is offline   Reply With Quote
Reply

Tags
execl, fault, fork, linux, output

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code output... roaan C Programming 6 07-03-2009 02:22 AM
Help for my output array qwertysingh C Programming 1 02-17-2009 03:08 PM
More digits in output, particular digits output, output in *.txt Ene Dene C++ Programming 4 11-30-2005 04:44 PM
Formatting output into even columns? Uncle Rico C Programming 2 08-16-2005 05:10 PM
Output problems with structures Gkitty C Programming 1 12-16-2002 05:27 AM


All times are GMT -6. The time now is 09:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22