Writing c code for who | sort | lp


 
Thread Tools Search this Thread
Top Forums Programming Writing c code for who | sort | lp
# 15  
Old 06-26-2007
Quote:
Originally Posted by the_learner
I really dont understand this pipe() and fork() and dup() and fnctl()... I think I need to buy that book which Perderabo suggested.. Advanced Unix Programming.
Okay, what you need to understand is what a process is in UNIX. Basically a process is

1. a memory image

2. a set of file descriptors 0,1,2 etc.

3. a point of execution

The memory image exists outside the kernel, the file descriptors live inside the kernel. A single file descriptor points to a file. That file may be referenced by multiple file descriptors.

Fact 1. All close() does is closes a single file descriptor for the current process. That drops the usage count on the actual file, when the usage count on the file drops to zero then it is really closed.

Fact 2. When a process terminates, all it's file descriptors that are still open are closed.

Fact 3. All "dup" and "dup2" do are allocate another file descriptor to point to the same file and increase the usage count on the underlying file.

Fact 4. Fork() duplicates the entire memory image AND the file descriptor table. Both processes now continue executing at the same point, the only difference being the parent got the pid of the child, the child got nothing. Duplicating the file descriptor table is like dup() has been called on each open file descriptor.

Fact 5. Exec() replaces the entire memory image with the new program but the file descriptors do not change EXCEPT for the file descriptors marked as FD_CLOEXEC, which are closed.

Fact 6. Pipe() returns two file descriptors, one is readable, one is writeable. A pipe will remain readable until all writeable file descriptors have been closed. Remember if you create a pipe then call fork there are two descriptors that are writeable.

Hints:

Get into the habit of putting -1 in variables that hold file descriptors when they are not holding a value, when you close it, put -1 back in the file descriptor variable.

Code:
int fd=-1;

fd=open(.....)

....stuff...

close(fd);

fd=-1;

# 16  
Old 06-26-2007
Quote:
Originally Posted by the_learner
But for some cases, if I leave some of the close statements in there.. it seems to work..
If you need it working, then use the working version.

Quote:
Originally Posted by the_learner
But I need to get this done urgently Smilie Porter please help...
Deadline approaching?

By the way, you must cope with execXX() failing. If the program is not found execlp (or whatever) will return. Follow it with _exit(1); as follows...

Code:
execlp(.....);
/* ERROR: could not find or run program */
_exit(1);

Using _exit would allow you to use vfork later.
# 17  
Old 07-04-2007
Quote:
Originally Posted by porter
Okay, what you need to understand is what a process is in UNIX. Basically a process is

1. a memory image

2. a set of file descriptors 0,1,2 etc.

3. a point of execution

The memory image exists outside the kernel, the file descriptors live inside the kernel. A single file descriptor points to a file. That file may be referenced by multiple file descriptors.

Fact 1. All close() does is closes a single file descriptor for the current process. That drops the usage count on the actual file, when the usage count on the file drops to zero then it is really closed.

Fact 2. When a process terminates, all it's file descriptors that are still open are closed.

Fact 3. All "dup" and "dup2" do are allocate another file descriptor to point to the same file and increase the usage count on the underlying file.

Fact 4. Fork() duplicates the entire memory image AND the file descriptor table. Both processes now continue executing at the same point, the only difference being the parent got the pid of the child, the child got nothing. Duplicating the file descriptor table is like dup() has been called on each open file descriptor.

Fact 5. Exec() replaces the entire memory image with the new program but the file descriptors do not change EXCEPT for the file descriptors marked as FD_CLOEXEC, which are closed.

Fact 6. Pipe() returns two file descriptors, one is readable, one is writeable. A pipe will remain readable until all writeable file descriptors have been closed. Remember if you create a pipe then call fork there are two descriptors that are writeable.

Hints:

Get into the habit of putting -1 in variables that hold file descriptors when they are not holding a value, when you close it, put -1 back in the file descriptor variable.


Hey thanks a lot Porter. This post has really cleared a lot of things for me. Anyway, I have left the working version of the code (you were right deadline was approaching.. Smilie But I really appreciate the time you took to explain things. Thanks again !
# 18  
Old 07-04-2007
Quote:
Originally Posted by the_learner
But I really appreciate the time you took to explain things. Thanks again !
No problem. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help writing code

Hi, i need a string that take from a log file just two words (ngwan0) and his related ip address and then execute 2 commands in a shell. Any ideas where to start? Thanks (3 Replies)
Discussion started by: Board27
3 Replies

2. Shell Programming and Scripting

Writing a UNIX script from LOG to provide return code.

Folks - Firstly, I do apologize that my first post here is a question. I am quite familiar with UNIX since our application is running on it. We are trying to automate a few things on our end and I am challenged with a task in hand that requires UNIX scripting. I am totally a newbie in UNIX... (4 Replies)
Discussion started by: sk72
4 Replies

3. Shell Programming and Scripting

Sort error code 512

Hi Friends, I am back with small issue which i tried thinking but could not get much!!!! issue here is..i m using kind of extraction query using perl code in that i do sort with few number of csv files. each of them will be around 50 to 100 M size. sometime i get following error message... (1 Reply)
Discussion started by: Shahul
1 Replies

4. UNIX for Dummies Questions & Answers

sed remove two headers; writing more elegant code

Hi there, I have two questions. First, I was wondering how to use sed to remove two header lines or two tail lines. Here I just do the same operation twice...I'm sure there is a better way. Second, and more importantly, is there a better way to have these operations use files other than... (5 Replies)
Discussion started by: mikey11415
5 Replies

5. Programming

Vi question for writing source code

Hi guys, I'm modifying an old f77 code using vi. I'm modifying the code remotely from my windows machine using xming using vi. I'm using tabs to move past the first 6 columns of the code and to keep my loops and if statements neat, but when I hit the tab key, vi displays a big red block which is... (7 Replies)
Discussion started by: rks171
7 Replies

6. Homework & Coursework Questions

Writing Code

1. The problem statement, all variables and given/known data: Write a script that will perform the grep command on a user defined file and a user defined pattern 2. Relevant commands, code, scripts, algorithms: must use grep command (that is the only piece of information that was given) ... (2 Replies)
Discussion started by: akjim101
2 Replies

7. Shell Programming and Scripting

script or piece of code where the data returned by a stored procedure you are writing

hi fndz. Can you please help me with the code if I call a stored procedure from my shell script and stored procedure returns a cursor, cursor output should be saved to a file (3 Replies)
Discussion started by: enigma_83
3 Replies

8. Shell Programming and Scripting

Help in writing the code

hi i am studying unix with c language ,and completely new to this .I have question about how to pass the control from one program file to another.My first program has the main process and it should go into the second program where i can create six processes with also the usage of pipes to... (3 Replies)
Discussion started by: soumya_v7
3 Replies

9. Shell Programming and Scripting

writing code!!!

suppose by writing this code "$ /sbin/ifconfig eth0| grep 'inet addr:' " i've got this output "inet addr:192.168.2.1 Mask:255.255.255.0" now if i want to see the output like this "IP address: 192.168.2.1 Subnet Mask: 255.255.255.0" what is the code should be? can anyone... (3 Replies)
Discussion started by: shelly
3 Replies

10. Shell Programming and Scripting

conditional writing of sql code

Hello again... I have a request from another department to list for them all the columns and tables we use in this certain database. I have spooled the oracle stored procedured into 1 file. I need a way to write out parts of that file. The criteria is to to start the block to be written when... (0 Replies)
Discussion started by: kburrows
0 Replies
Login or Register to Ask a Question