LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unix system calls with c (https://www.linuxquestions.org/questions/programming-9/unix-system-calls-with-c-232239/)

pengui 09-18-2004 03:46 PM

Unix system calls with c
 
I am a beginner and have RH 8 installed. We have UNIX Lab and i have to do c programs with the help of unix system calls.... caan u plz help me,,,
Is gcc enough to compile a c program ... plz help??

Mara 09-18-2004 05:13 PM

Yes, it is. When you have your code written, let's say in myprog.c you can compile it using
gcc -o progname myprog.c
The executable created will be 'progname'.

Do you have more specific questions? I'm not sure if I understand what you mean.

pengui 09-19-2004 12:42 AM

I mean i had to generate some commands like cat,grep with the help of unix system calls.
I have to include the header files fcntl.h,unistd.h.. Is all these header files availabe in Linux.. and where i have to type the code? Can i compile it in any dir?

mirradric 09-19-2004 12:47 AM

What do you mean by generate??
I think you probably mean executing them from C. Am I right?
or are you trying to write programs that duplicate their functions? I suspect you'll not be posting this if that's the case.

Quote:

Originally posted by pengui
I mean i had to generate some commands like cat,grep with the help of unix system calls.
I have to include the header files fcntl.h,unistd.h.. Is all these header files availabe in Linux.. and where i have to type the code? Can i compile it in any dir?


mirradric 09-19-2004 12:51 AM

I probably misunderstood your question.
Yes, all these headers are available.

gr33ndata 09-19-2004 02:42 AM

Sounds that you wanna execute those commands from within a C prog. So you may use commands like "system" and the "exec" family. do man system and man exec for more info. However for simplicity to run ls for example do write system("ls");

shrey_j 09-19-2004 11:47 PM

Hi pengui,

Possibly all the people above may have solved your problem, still I am posting this because even I was at this stage someday.

See gcc is a compiler in Linux, which helps compile C files, no matter where these files exists (in any directory), only thing is that you should be able to run gcc from that. [type gcc and if the output on screen is "no files specified", it is perfect]. HEader files are kept in /usr/include/ directory, and there are a large no of them including the above ones.

As for your program , here is a sample snippet:

//test.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
system(" ls -l");
return 0;
}

you can replace whatever command in " " in system call. like cat, grep etc. [but please take care not to use ' " ' anywhere or they will create problem. use \" if you need to have ' " ' within your command.

File name of above prog is file.c so: type
gcc file.c -o file; which will create a output file 'file' in the same directory as you compiled in. execute it by ' ./file '. ./ is required to tell bash to execute from the local directory.

if it does not work and some problem of header files exists: use
gcc file.c -o file -I/usr/include/ where you can replace /usr/include/ with the path where gcc can find its header files [if you know where they are.

Hope it helps.

suowei1979 09-20-2004 01:43 AM

:)

pengui 09-20-2004 05:32 AM

Quote:

Originally posted by shrey_j
Hi pengui,

Possibly all the people above may have solved your problem, still I am posting this because even I was at this stage someday.

See gcc is a compiler in Linux, which helps compile C files, no matter where these files exists (in any directory), only thing is that you should be able to run gcc from that. [type gcc and if the output on screen is "no files specified", it is perfect]. HEader files are kept in /usr/include/ directory, and there are a large no of them including the above ones.

As for your program , here is a sample snippet:

//test.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
system(" ls -l");
return 0;
}

you can replace whatever command in " " in system call. like cat, grep etc. [but please take care not to use ' " ' anywhere or they will create problem. use \" if you need to have ' " ' within your command.

File name of above prog is file.c so: type
gcc file.c -o file; which will create a output file 'file' in the same directory as you compiled in. execute it by ' ./file '. ./ is required to tell bash to execute from the local directory.

if it does not work and some problem of header files exists: use
gcc file.c -o file -I/usr/include/ where you can replace /usr/include/ with the path where gcc can find its header files [if you know where they are.

Hope it helps.

Than you shrey_j for your deatiled reply...

sudevdev 07-28-2012 04:25 AM

Materials for learning C programing for unix system calls
 
Hey,

I'm second year B tech student, in our curriculum we have UNIX programming alb where we have to write system calls using C/ C++ programming. I was wondering if you could help me in recommending some materials for writing system calls using C program ?

Please help Urgent !

I can write C programs, I just need the specific thing for writing system calls ( basically writing programs for UNIX environment )

NevemTeve 07-28-2012 07:32 AM

You shouldn't write system calls, only use them. Examples: open/close/read/write (later: lseek/ioctl/fcntl etc). See the manual.

tushar_pandey 07-29-2012 01:07 AM

Thanks
 
Thanks for your help

Quote:

Originally Posted by shrey_j (Post 1185799)
Hi pengui,

Possibly all the people above may have solved your problem, still I am posting this because even I was at this stage someday.

See gcc is a compiler in Linux, which helps compile C files, no matter where these files exists (in any directory), only thing is that you should be able to run gcc from that. [type gcc and if the output on screen is "no files specified", it is perfect]. HEader files are kept in /usr/include/ directory, and there are a large no of them including the above ones.

As for your program , here is a sample snippet:

//test.c
#include<stdio.h>
#include<stdlib.h>
int main()
{
system(" ls -l");
return 0;
}

you can replace whatever command in " " in system call. like cat, grep etc. [but please take care not to use ' " ' anywhere or they will create problem. use \" if you need to have ' " ' within your command.

File name of above prog is file.c so: type
gcc file.c -o file; which will create a output file 'file' in the same directory as you compiled in. execute it by ' ./file '. ./ is required to tell bash to execute from the local directory.

if it does not work and some problem of header files exists: use
gcc file.c -o file -I/usr/include/ where you can replace /usr/include/ with the path where gcc can find its header files [if you know where they are.

Hope it helps.


sudevdev 07-29-2012 11:32 PM

Quote:

Originally Posted by NevemTeve (Post 4739975)
You shouldn't write system calls, only use them. Examples: open/close/read/write (later: lseek/ioctl/fcntl etc). See the manual.

Thanks for your reply ....

But they are asking us to write the primitive function in C (of the read write system calls). Is it really tough to wirte these primitive functions ?

NevemTeve 07-29-2012 11:54 PM

For a start, read AST's books.


All times are GMT -5. The time now is 01:26 AM.