Handling I/O redirections in my shell.

This is a discussion on Handling I/O redirections in my shell. within the Linux Programming forums, part of the Platform Specific Boards category; Here's my handler: Code: void handle_INT () { printf ("An interrupt has occured...\n"); printf ("\n\n\n\n\n\n\n\n\n"); longjmp (jmp, 1); } ok, ...

  1. #31
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    Here's my handler:
    Code:
    void handle_INT ()
    {
    	printf ("An interrupt has occured...\n");
    	printf ("\n\n\n\n\n\n\n\n\n");
    	longjmp (jmp, 1);
    }
    ok, a couple of things:
    - I want to reuse the old handler so i have to save the return value of signal. I try to save it in sig (of type struct sighandler_t) and i get "sig has incompatible type". I can't change the handler's return type since a return type of struct sighandler_t gives me incompatible type for arg2 of signal. What do i do?

    -i am not using longjmp to get out of a loop. My shell proceses the commands and then exectues them. If it encounters a SIGINT during the processing, it needs to clear the command line and reask the user to enter a command. So i ahev to go back to the start of the main loop inorder to re-ask the user to...

    The problem is the instructor gave me these notes and tutorials, so i'm guessing they must be enough. You guys are talking about standards which i have no clue about.

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Be careful when longjmping out of a signal handler.
    http://www.gnu.org/software/libc/man...n-Handler.html

    What libc is installed on your system? For that matter, exactly what system is it? It seems to have some odd quirks.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #33
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    It is 2.3.3. I'm using Fedora core 3.

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Very odd.

    You might want to use sigaction instead -- that should have more consistent semantics across systems.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #35
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    But i can't. I need to use the ones specified in the assignment requirements.

    I think i'm gonna leave aside for a while. But back to the original topic, if i want to build: "ls | wc"
    Is it just a matter of connecting the child processes via a pipe or do i also need to redirect some stuff?
    I need to redirect the stdout of ls and stdin of wc, correct?

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Correct, and they need to be the two ends of one pipe.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #37
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by sbho View Post
    ok, a couple of things:
    - I want to reuse the old handler so i have to save the return value of signal. I try to save it in sig (of type struct sighandler_t) and i get "sig has incompatible type". I can't change the handler's return type since a return type of struct sighandler_t gives me incompatible type for arg2 of signal. What do i do?
    It sounds a lot like you aren't including <signal.h>. If you are, then there's nothing more to go on. You haven't shown anywhere near sufficient code (for instance, all the headers you include and your own prototypes) nor given the EXACT compiler error message.

  8. #38
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    Quote Originally Posted by CornedBee View Post
    Correct, and they need to be the two ends of one pipe.
    Well i'm having a problem with the code. The 2nd program is not waiting for the first program to send the input.
    Code:
    pipe (p);
    	pid1 = fork();
    	if (pid1 == 0)
    	{
    		close (p[0]);	// close the pipe input
    		close (1);	//close stdout
    		dup (p[1]);	//redirect stdout to output of pipe
    		execvp (*argv1, argv1);
    		printf ("Error: Could not execute the 1st program\n");
    		exit (1);
    	}
    	else if (pid1 != 0)
    	{
    		pid2 = 	fork();
    		if (pid2 == 0)
    		{
    			close (p[1]);	// close the pipe output
    			close (0);	//close stdin
    			dup (p[0]);	//redirect stdin to input of pipe
    			execvp (*argv2, argv2);
    			printf ("Error: Could not execute the 2nd program\n");
    			fflush (stdout);
    			exit (1);
    		}
    		
    	}
    Both programs start execution, so there are no problems with the args.
    The programs i'm using are:
    Code:
    prog1
    #include <stdio.h>
    
    int main ()
    {
    	printf ("%s",system ("ls -1"));
    	return 0;
    }
    Code:
    prog2
    #include <stdio.h>
    
    int main ()
    {
    	char buff[1024];
    	while (fgets(buff, 1024, stdin) != NULL)
    	{
    		printf ("This is it: %s--\n", buff);
    	}
    	return 0;
    }
    What have i done wrong?
    Thanks.

  9. #39
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    It's working now...i have no clue why.
    But i have a question: If i pass a pointer as a parameter to a function, can i free that pointer inside the function?
    Thanks.

  10. #40
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by sbho View Post
    If i pass a pointer as a parameter to a function, can i free that pointer inside the function?
    Depends who "owns" the data the pointer points to. The code which frees a pointer is considered to be the owner of that pointer. So when a higher level function passes a pointer to a lower level one, and the lower level function frees it, that implies that the call to the function transfers ownership of the pointer.

    So as you design this kind of code, draw a diagram which describes how blocks of data are created and managed, and how they flow to the code which ultimately frees them. Don't make it too complicated, or other programmers will have serious problems working with your code.

  11. #41
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > printf ("&#37;s",system ("ls -1"));
    Since system() returns an int, what does %s in printf do?

    While you're at it, start with two simple processes
    Code:
    // producer.c
    #include <stdio.h>
    #include <stdlib.h>
    int main ( ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
        printf("Line %d\n", i );
        sleep( 1 );
      }
      return 0;
    }
    Code:
    // consumer.c
    #include <stdio.h>
    
    int main ()
    {
    	char buff[1024];
    	while (fgets(buff, 1024, stdin) != NULL)
    	{
    		printf ("This is it: %s--\n", buff);
    	}
    	return 0;
    }
    Then from the command line, observe the behaviour of
    producer | consumer

    Then look for the same behaviour using your shell.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. File I/O both ASCII and binary handling in C++
    By random in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2005, 04:53 AM
  4. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 10:47 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

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