LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-18-2004, 03:46 PM   #1
pengui
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Rep: Reputation: 0
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??
 
Old 09-18-2004, 05:13 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
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.
 
Old 09-19-2004, 12:42 AM   #3
pengui
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Original Poster
Rep: Reputation: 0
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?
 
Old 09-19-2004, 12:47 AM   #4
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
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?
 
Old 09-19-2004, 12:51 AM   #5
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
I probably misunderstood your question.
Yes, all these headers are available.
 
Old 09-19-2004, 02:42 AM   #6
gr33ndata
Member
 
Registered: Aug 2003
Location: DMZ
Distribution: Ubuntu
Posts: 144

Rep: Reputation: 15
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");
 
Old 09-19-2004, 11:47 PM   #7
shrey_j
Member
 
Registered: Jul 2004
Location: Delhi
Distribution: FC3, RH 9.0
Posts: 39

Rep: Reputation: 16
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.
 
1 members found this post helpful.
Old 09-20-2004, 01:43 AM   #8
suowei1979
LQ Newbie
 
Registered: Sep 2004
Location: BeiJing,China
Distribution: Red Hat
Posts: 25

Rep: Reputation: 16
 
1 members found this post helpful.
Old 09-20-2004, 05:32 AM   #9
pengui
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Original Poster
Rep: Reputation: 0
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...
 
Old 07-28-2012, 04:25 AM   #10
sudevdev
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
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 )

Last edited by sudevdev; 07-28-2012 at 04:27 AM.
 
Old 07-28-2012, 07:32 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,715

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
You shouldn't write system calls, only use them. Examples: open/close/read/write (later: lseek/ioctl/fcntl etc). See the manual.
 
Old 07-29-2012, 01:07 AM   #12
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
Thanks

Thanks for your help

Quote:
Originally Posted by shrey_j View Post
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.
 
Old 07-29-2012, 11:32 PM   #13
sudevdev
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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 ?
 
Old 07-29-2012, 11:54 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,715

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
For a start, read AST's books.
 
  


Reply

Tags
gcc


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System calls question alltime Programming 1 11-29-2005 08:31 PM
Some system calls Spooky Programming 1 11-24-2004 10:17 AM
How to Write Own Shell in Unix using System Calls? indian Programming 2 08-05-2004 04:06 AM
System Calls file Hady Programming 1 12-13-2003 12:14 PM
java and system calls aizkorri Programming 6 06-20-2003 01:54 AM


All times are GMT -5. The time now is 04:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration