LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-13-2015, 01:14 PM   #16
goldriver92
LQ Newbie
 
Registered: Dec 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled

I know how to fork and create child processes but what's it got to do with invoking a printing command? I can't believe that such a simple task is taking so much time.
 
Old 01-13-2015, 01:25 PM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by goldriver92 View Post
I know how to fork and create child processes but what's it got to do with invoking a printing command? I can't believe that such a simple task is taking so much time.
/usr/bin/lpr and /usr/bin/lp are binary commands which you can run and you can run them via an exec() call. Since you know how to fork() and exec(), what's the problem?

You are within a C program, at least that's what I thought you started saying. Create an argument list, fork() a child, and then run exec() using either lpr or lp and passing it the argument list. This recommendation is exactly what you asked, which was:
Quote:
So how does one invoke the lpr command or lp command in a c program?
A: You use fork() and exec(). I offered "an" example, and suggested also that a general web search for examples may also give you some guidance.

And NevemTeve said the same recommendation back in post #9.

Last edited by rtmistler; 01-13-2015 at 01:26 PM.
 
1 members found this post helpful.
Old 01-13-2015, 01:42 PM   #18
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Completely untested (and ugly) code (I don't have a local printer on my Linux box)
Code:
#include <stdio.h>
int main(){
    FILE *in = fopen("/home/user/Desktop/texts/abc.txt", "r");
    FILE *pr = fopen("/dev/lp0", "w"); //Try /dev/usb/lp0 if you have USB printer
    if(!in){printf("Failed to open input file\n"); return -1;}
    if(!pr){printf("Failed to open printer\n"); return -1;}
    while(!feof(in)){
         fputc(fgetc(in), pr);
    }
    return 0;
}
 
Old 01-13-2015, 02:12 PM   #19
goldriver92
LQ Newbie
 
Registered: Dec 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
i have a usb printer hp laser jet 1020 attached with my ubuntu desktop. Entering the command on the terminal ,lp filename gets the job done as far as the terminal is concerned. But the real challenge is to invoke command from a C program.
 
Old 01-13-2015, 02:31 PM   #20
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by goldriver92 View Post
I know how to fork and create child processes
Apparently you don't and you're not even looking at links or any information you've been referred too.
Quote:
Originally Posted by goldriver92 View Post
i have a usb printer hp laser jet 1020 attached with my ubuntu desktop. Entering the command on the terminal ,lp filename gets the job done as far as the terminal is concerned. But the real challenge is to invoke command from a C program.
You've been told by two different people and you've claimed to know how to do the actions you were recommended to do. I'm suspecting you just don't want to invest the time.

Just so you don't "go off" although that's still likely.

What EXACT shell command do you issue to print that file as you're describing above? "lp filename" is one example.

And what follows is an incomplete example, but one which will work. Incomplete because you're just saying "I can't do it" as well as "This is so difficult"; so start with this and if you have questions, send them along. If you don't follow this and persist with saying this is so difficult, sorry but you're beyond any assistance level I can provide you at this time.

Code:
char *args[3] = { "/usr/bin/lp", "filename", NULL };
pid_t pid;
char *env[] = { NULL };

pid = fork();
if(pid == 0) {
    execve("/usr/bin/lp", args, env);
}

Last edited by rtmistler; 01-13-2015 at 02:34 PM.
 
1 members found this post helpful.
Old 01-13-2015, 03:41 PM   #21
goldriver92
LQ Newbie
 
Registered: Dec 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
First i would like to thank you for taking the time out and answering my questions . I am sorry if it sounds like i don't want to invest time into this problem whereas i have spent 2 nerve wrecking days on multiple forums posting the problem and not getting a satisfactory answer from any place. I know forking as i have created child processes before to handle multiple clients wanting a service from a serve. In that sense, i know forking but i do not know how to use them with exec() command plus what is the objective of creating a child?
why can't i just use this?



system("lp /home/user/Desktop/xfolder/network.txt");
Someone told me that this system command will hang my code whereas my server is multi-threaded and it would probably have multiple clients using the print service which means it wants to go back to the listening state as soon as it sends out the system command.







1.Posting the printer thing again was to let smeezekitty know my exact problem.
 
Old 01-13-2015, 03:44 PM   #22
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Someone told me that this system command will hang my code whereas my server is multi-threaded and it would probably have multiple clients using the print service which means it wants to go back to the listening state as soon as it sends out the system command.
The system() command is quite inelegant but it can be used. It is a blocking call which means your code will
stop until the started process ends EXCEPT I believe you can add an & to the end to run it in the background.

Code:
 system("lp /home/user/Desktop/xfolder/network.txt &");
 
Old 01-13-2015, 05:15 PM   #23
goldriver92
LQ Newbie
 
Registered: Dec 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Are you suggesting that a threaded system call function would still stall the code?

is there a way to queue system calls sent by different clients to our multi-threaded server?
 
Old 01-13-2015, 05:55 PM   #24
goldriver92
LQ Newbie
 
Registered: Dec 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Can we pass variables to system calls?
i tried to pass a file path which was stored in a variable but it didn't recognize it.
 
Old 01-13-2015, 06:11 PM   #25
goldriver92
LQ Newbie
 
Registered: Dec 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
@rtmstiler THANKS A LOT! the fork and exec thing is working!!
 
Old 01-13-2015, 08:51 PM   #26
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by goldriver92 View Post
Can we pass variables to system calls?
i tried to pass a file path which was stored in a variable but it didn't recognize it.
Look up sprintf
 
Old 01-14-2015, 12:30 AM   #27
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
system is the easiest way, but it requires a little work to be safe even with unusual file-names:

Code:
setenv ("PARAM_1", file_name);
system ("/usr/bin/lpr other options - \"$PARAM_1\" &);
 
  


Reply

Tags
clanguage, linux, printer



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
[SOLVED] Program to print hdd manufacturer name. stf92 Linux - Software 5 01-19-2011 06:00 AM
[SOLVED] A better program to print pdf files. taylorkh Linux - Software 1 09-30-2010 09:46 AM
a program to print photos from brotell Ubuntu 4 01-28-2007 05:34 PM
Program to print out pictures frankieboy Linux - Software 2 11-01-2003 07:52 AM
Any program to print CD-Covers? Mega Man X Linux - Software 2 07-22-2003 03:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration