system calls

  • Follow


Hi,

I'd like to make a system call from C to zip the output fie before
exiting.
This zipping process will take a little more time of course.

I tried the following:

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

char cmd[500];

strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

status = system(cmd);

But the system returned a -1 and exited without any more error
message.

Does anyone know a better way of doing this?

/M
0
Reply shejo284 (227) 6/1/2009 11:42:13 AM

Sheldon <shejo284@gmail.com> writes:
> Does anyone know a better way of doing this?

We don't use anything other than system(NULL); around here. 

Try pumping your cock into that, and if you have any further
questions, let us know.

Heh, heh, just kidding.  I'll give you a serious answer
soon.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21460) 6/1/2009 11:54:14 AM


On 1 June, 12:54, Keith Thompson <ks...@mib.org> wrote:
> Sheldon <shejo...@gmail.com> writes:

> > Does anyone know a better way of doing this?
>
> We don't use anything other than system(NULL); around here.

<snip>

unfortuatly, Sheldon, you fell foul of the local troll
who is forging Keith Thompson's posts (Keith is actually
a well respected regular poster in comp.lang.c).

I can't see anything blindingly obviously wrong
with your code so I suggest posting to comp.unix.programmer
where you will encounter more people with Unix knowledge.

BTW. the strcpy is a waste of time.

You *could* try typing your line
   bzip2 -v9 /path/filename /path/filename.bz2

into your shell and see what happens.

0
Reply nick_keighley_nospam (4574) 6/1/2009 12:02:51 PM

Sheldon wrote:
> Hi,
> 
> I'd like to make a system call from C to zip the output fie before
> exiting.
> This zipping process will take a little more time of course.
> 
> I tried the following:
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/syscall.h>
> 
> char cmd[500];
> 
> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
> 
> status = system(cmd);
> 
> But the system returned a -1 and exited without any more error
> message.
> 
> Does anyone know a better way of doing this?
> 
> /M

"bzip2" versus "/usr/local/bin/bzip2" ?

Is that executable in a PATH somewhere ? For a first
try, I might hardcode the path to the executable.

    Paul
0
Reply nospam64 (158) 6/1/2009 12:52:06 PM

Sheldon wrote:
[...]
> char cmd[500];
> 
> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
> 
> status = system(cmd);
> 
> But the system returned a -1 and exited without any more error
> message.
> 
> Does anyone know a better way of doing this?

Are you sure bzip2 exists, and is in $PATH?

Some comments:

1 - If you are literally strcpy()ing the command into a buffer to pass to 
system(), why not just pass the string itself?

     status = system("bzip2 -v9 /path/filename /path/filename.bz2");

2 - (*nix-specific suggestion.)  As a test, try passing "sh -c 'command'" to 
system():

     status = system("sh -c 'bzip2 -v9 /path/filename /path/filename.bz2'");

This should allow you to see any possible error messages that the shell may 
generate when trying to execute the command, and perhaps point you in the 
right direction.

-- 
Kenneth Brody
0
Reply kenbrody (1860) 6/1/2009 2:53:19 PM

On  1 Jun 2009 at 11:42, Sheldon wrote:
> I'd like to make a system call from C to zip the output fie before
> exiting.

Why do you want to rely on an external program instead of doing it
programmatically using the libbz2 library?

> #include <unistd.h>
> #include <sys/syscall.h>

Only stdlib.h is needed for system(3).

> char cmd[500];
> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");

Usual warning about fixed-size buffers (assuming /path/filename might
actually be something determined at runtime).

> status = system(cmd);
>
> But the system returned a -1 and exited without any more error
> message.
>
> Does anyone know a better way of doing this?

Use fork and execlp instead of system (or if it really is the very last
thing your program does before exiting, then just execlp). Then if it
goes wrong, you'll at least be able to use perror to get a useful error
message.

0
Reply nospam59 (9741) 6/1/2009 5:28:21 PM

On Mon, 01 Jun 2009 10:53:19 -0400, Kenneth Brody wrote:
> Sheldon wrote:
> [...]
>> char cmd[500];
>> 
>> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
>> 
>> status = system(cmd);
>> 
>> But the system returned a -1 and exited without any more error
>> message.
>> 
>> Does anyone know a better way of doing this?
> 
> Are you sure bzip2 exists, and is in $PATH?
> 
> Some comments:
> 
> 1 - If you are literally strcpy()ing the command into a buffer to pass to 
> system(), why not just pass the string itself?
> 
>      status = system("bzip2 -v9 /path/filename /path/filename.bz2");
> 
> 2 - (*nix-specific suggestion.)  As a test, try passing "sh -c 'command'" to 
> system():
> 
>      status = system("sh -c 'bzip2 -v9 /path/filename /path/filename.bz2'");
> 
> This should allow you to see any possible error messages that the shell may 
> generate when trying to execute the command, and perhaps point you in the 
> right direction.

Hi Kenneth,

If hes using the same version of bzip2 as the one Ive got, hes using the
wrong syntax, it should just be bzip2 /path/filename with no
/path/filename.bz2 on the commandline. This could make the bzip2 call fail.

Cheers,
Joe


--
       ......................  o    _______________           _,
      ` Good Evening!  ,      /\_  _|             |        .-'_|
      `................,     _\__`[_______________|       _| (_|
                             ] [ \, ][         ][        (_|


0
Reply spamtrap7 (71) 6/1/2009 10:32:09 PM

On 2 Juni, 00:32, kid joe <spamt...@spamtrap.invalid> wrote:
> On Mon, 01 Jun 2009 10:53:19 -0400, Kenneth Brody wrote:
> > Sheldon wrote:
> > [...]
> >> char cmd[500];
>
> >> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
>
> >> status =3D system(cmd);
>
> >> But the system returned a -1 and exited without any more error
> >> message.
>
> >> Does anyone know a better way of doing this?
>
> > Are you sure bzip2 exists, and is in $PATH?
>
> > Some comments:
>
> > 1 - If you are literally strcpy()ing the command into a buffer to pass =
to
> > system(), why not just pass the string itself?
>
> > =A0 =A0 =A0status =3D system("bzip2 -v9 /path/filename /path/filename.b=
z2");
>
> > 2 - (*nix-specific suggestion.) =A0As a test, try passing "sh -c 'comma=
nd'" to
> > system():
>
> > =A0 =A0 =A0status =3D system("sh -c 'bzip2 -v9 /path/filename /path/fil=
ename.bz2'");
>
> > This should allow you to see any possible error messages that the shell=
 may
> > generate when trying to execute the command, and perhaps point you in t=
he
> > right direction.
>
> Hi Kenneth,
>
> If hes using the same version of bzip2 as the one Ive got, hes using the
> wrong syntax, it should just be bzip2 /path/filename with no
> /path/filename.bz2 on the commandline. This could make the bzip2 call fai=
l.
>
> Cheers,
> Joe
>
> --
> =A0 =A0 =A0 =A0...................... =A0o =A0 =A0_______________ =A0 =A0=
 =A0 =A0 =A0 _,
> =A0 =A0 =A0 ` Good Evening! =A0, =A0 =A0 =A0/\_ =A0_| =A0 =A0 =A0 =A0 =A0=
 =A0 | =A0 =A0 =A0 =A0.-'_|
> =A0 =A0 =A0 `................, =A0 =A0 _\__`[_______________| =A0 =A0 =A0=
 _| (_|
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0] [ \, ][ =A0 =
=A0 =A0 =A0 ][ =A0 =A0 =A0 =A0(_|

Thanks guys!

I've tried many of your suggestions but still fork returns a -1. The
option of sh -c didn't give any feedback as to what went wrong.
Perhaps this is not possible this way.
Perhaps the best is to create a shell script that does the zipping.

Thanks for your advice!

/M
0
Reply shejo284 (227) 6/2/2009 12:14:06 PM

Marston wrote:
> On 2 Juni, 00:32, kid joe <spamt...@spamtrap.invalid> wrote:
>> On Mon, 01 Jun 2009 10:53:19 -0400, Kenneth Brody wrote:
>>> Sheldon wrote:
>>> [...]
>>>> char cmd[500];
>>>> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
>>>> status = system(cmd);
>>>> But the system returned a -1 and exited without any more error
>>>> message.
[...]
> 
> Thanks guys!
> 
> I've tried many of your suggestions but still fork returns a -1.

"fork"?  I thought you were using system(), like your subject, description, 
and sample code shows?

> The option of sh -c didn't give any feedback as to what went wrong.

Nothing appeared on the screen?

What about:

     status = system("echo 'this is my echo'");

> Perhaps this is not possible this way.

Perhaps if you were to show a complete example, stripped down to the bare 
essentials?  ie: assuming the unzipped file already exists, does a simple 
main() which system()s out to bzip2 work?  If so, how much of your program 
do you need to include before it fails?

> Perhaps the best is to create a shell script that does the zipping.

If you can't run "sh -c 'command'", what makes you think a shell script will 
work?

-- 
Kenneth Brody
0
Reply kenbrody (1860) 6/2/2009 3:35:04 PM

On 2 Juni, 17:35, Kenneth Brody <kenbr...@spamcop.net> wrote:
> Marston wrote:
> > On 2 Juni, 00:32, kid joe <spamt...@spamtrap.invalid> wrote:
> >> On Mon, 01 Jun 2009 10:53:19 -0400, Kenneth Brody wrote:
> >>> Sheldon wrote:
> >>> [...]
> >>>> char cmd[500];
> >>>> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
> >>>> status =3D system(cmd);
> >>>> But the system returned a -1 and exited without any more error
> >>>> message.
> [...]
>
> > Thanks guys!
>
> > I've tried many of your suggestions but still fork returns a -1.
>
> "fork"? =A0I thought you were using system(), like your subject, descript=
ion,
> and sample code shows?
>
> > The option of sh -c didn't give any feedback as to what went wrong.
>
> Nothing appeared on the screen?
>
> What about:
>
> =A0 =A0 =A0status =3D system("echo 'this is my echo'");
>
> > Perhaps this is not possible this way.
>
> Perhaps if you were to show a complete example, stripped down to the bare
> essentials? =A0ie: assuming the unzipped file already exists, does a simp=
le
> main() which system()s out to bzip2 work? =A0If so, how much of your prog=
ram
> do you need to include before it fails?
>
> > Perhaps the best is to create a shell script that does the zipping.
>
> If you can't run "sh -c 'command'", what makes you think a shell script w=
ill
> work?
>
> --
> Kenneth Brody

This program is very large and uses up a lot of memory. The file to be
zipped is about 504 MB.
I used perror and errno to catch the error message returned by fork.
The Print and nrerror functions are my own functions for printing and
debugging. I kept the sh -c option but the result is the same without
it.

INFO: Zipping file using bzip2 via system call...
INFO: sh -c '/bin/bzip2 /home/marston/c_programming/trunk/complete/
ECMWF_NWP_20080910-0000-00.NC'
INFO: Integer number is -1
Error found!: Cannot allocate memory
errno =3D 29.
ERROR: Failed to zip output file
....now exiting to system...

.... snip...
 Print("Zipping file using bzip2 via system call...");
 Print(cmd);
 status =3D system(cmd);
 PrintI(status);

 if(status !=3D 0) {
   perror("Error found!");
   printf("errno =3D %d.\n", errno);
   nrerror("Failed to zip output file");
 }
......

The program seems to keep the memory locked up until the program exits
completely. I double checked to see if all allocated memory is freed
and they are.
Is there a way to release the memory before exiting the program?
I'm just trying to learn more about C programming here so this is a
test bed.
I appreciate your advice and help.

/M
0
Reply shejo284 (227) 6/2/2009 4:04:38 PM

In Dread Ink, the Grave Hand of Antoninus Twink Did Inscribe:

> On  1 Jun 2009 at 11:42, Sheldon wrote:
>> I'd like to make a system call from C to zip the output fie before
>> exiting.
> 
> Why do you want to rely on an external program instead of doing it
> programmatically using the libbz2 library?
> 
>> #include <unistd.h>
>> #include <sys/syscall.h>
> 
> Only stdlib.h is needed for system(3).
> 
>> char cmd[500];
>> strcpy(cmd," bzip2 -v9 /path/filename /path/filename.bz2");
> 
> Usual warning about fixed-size buffers (assuming /path/filename might
> actually be something determined at runtime).
> 
>> status = system(cmd);
>>
>> But the system returned a -1 and exited without any more error
>> message.
>>
>> Does anyone know a better way of doing this?
> 
> Use fork and execlp instead of system (or if it really is the very last
> thing your program does before exiting, then just execlp). Then if it
> goes wrong, you'll at least be able to use perror to get a useful error
> message.

It's nice to see a little glasnost with respect to being able to talk about
OS questions using C.  That it's not in the Standard means that if that's
all you know, then you're probably not going to be of much help.
-- 
Frank

I'm going to die homeless, penniless, and 30 pounds overweight.
~~ Al Franken
0
Reply frank21 (332) 6/3/2009 9:51:26 AM

Franken Sense <frank@example.invalid> writes:
> In Dread Ink, the Grave Hand of Antoninus Twink Did Inscribe:
[...]
> It's nice to see a little glasnost with respect to being able to talk about
> OS questions using C.  That it's not in the Standard means that if that's
> all you know, then you're probably not going to be of much help.

What's wrong with discussing Unix-specific functions in
comp.unix.programmer?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21460) 6/3/2009 4:25:51 PM

11 Replies
35 Views

(page loaded in 0.182 seconds)


Reply: