Whether should i use processes or threads in Linux?
While designing any system usually we might confuse between thread and process usage . We think should we use thread or process for our requirement. Which will be the best choice.?
So Here are some guidelines to help you……
- All threads inside a program always run from the same executable binary. Where a child process , may run a from different executable binary by calling an exec function.
- If one process crashes or has a buffer overrun, it does not affect any other process at all, whereas if a thread crashes, it takes down all of the other threads in the process because threads share the same virtual memory space and other resources. read more
Convert file descriptor to file pointer | Convert file pointer to file descriptor in Linux by C programming
If you all ready know the difference between file descriptor and file pointer then good otherwise please read this post
Whats is difference between file descriptor and file pointer?
File descriptor to file pointer :
for converting file descriptor to file pointer in linux fdopen() function is used
#include <stdio.h>
FILE *fdopen(int fildes, const char *mode);
fildes is file descriptor
mode is the character value which indicate the mode of file operation (same as we use in fopen() function)
Example :
#include <stdio .h> #include <fcntl .h> int main(){ int fd; FILE *fp; fd = open("shareprogrammingtips.txt",O_WRONLY | O_CREAT | O_TRUNC); if(fd<0) { printf("open call fail"); return -1; } fp=fdopen(fd,"w"); fprintf(fp,"we got file pointer fp bu using File descriptor fd"); fclose(fp); close(fd); return 0; } </fcntl></stdio> |
How can i detect Windows or Linux in C,C++ programming?
Sometimes we need to write platform independent code which works in windows and linux machine without giving any flag or any modification in code or make file manually.
So in such case i usually use compiler generated macro to check the platform .
In Linux all c and c++ compiler define macro like linux
, In Windows all c and c++ compiler define macro like _WIN32
so we can use this macro to separate code for linux and windows see belows example code
How can I get what my main function has returned in Linux?
In a C program if we want to give some input from terminal then we can give it by:
int main(int argc, char *argv[])
In the same way, if we want to get return value of main() function then how can we get it?
In each main() we write return 1 or return 0; how can we know what my main() has returned at terminal?
Why fopen() doesn’t get fail by passing directory name in argument?
Once i have made one small application where i was passing full file name as command line argument in my application where i am going to fopen() that file BUT once i have just passed any directory name instad of file name still my fopen() does not get fails…!!
#include<stdio.h> #include <errno.h> int main() { errno = 0; FILE *fb = fopen("/home/jeegar/","r"); if(fb==NULL) printf("its null"); else printf("working"); printf("Error %d \n", errno); } |
output is
workingError 0 |
here fb is not going null and also not getting any error..
Whats is difference between file descriptor and file pointer?
After having 1 year of experience in c programming when someone has asked this to me i was surprised that why i dont know this…!!
Well when i will going to interview someone in someday this would be my 1st question to him/her…!!!
File Descriptor :
“A file descriptor is a low-level integer “handle” used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems.”
int fd; fd = open(path, O_CREAT | O_RDONLY, 0644); close(fd); |
This way you are using that is file-descriptor. In short, file-descriptor is
- Low/Kernel level handler
- passe to read() and write() of UNIX System Calls
- Doesn’t include buffering and such features
- Less portable and lacks efficiency
- It’s some what OS specific so its not part of standard c programming language.
how to increase stack size in linux.
Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:
- call getrlimit to get current stack size
- if current size < required stack size then
- call setrlimit to increase stack size to required size
In C that might be coded something like this:
read more
Equivalent to getch() & getche() in Linux
Once in one situation i need to use getch() and getche() function in Linux based application so i have searched a lot for Equivalent header file for conio.h in Linux,But i came to know there is not any header file/library equivalent to conio.h in linux.
I want to make a switch case base menu where the user will give his option just by pressing one key & process should be moved ahead. I don’t want to let user to press ENTER after pressing his choice.
Somebody has suggest me very magical code for getch() and getche() function for linux which is i am sharing here. read more
Top/Best C/C++ IDE for Windows & Linux
How much time gone in project design & building logic that much time also gone in writing code So if you have better IDE then you can work faster and easily. So i am going to introduce you most famous IDE for C/C++ programming on Linux & windows.
1>Eclipse CDT (C/C++ Development Tooling)
Find and replace command for whole directory in Linux
In many case most of the projects have so many files in so many different folder. So at some point you want to do find and replace for particular word in your whole project or whole directory in that case this command will help you.
In linux for doing this there is no single command but we can design one new command by combination of several.
find /path/to/directory -type f -exec sed -i.bak 's/oldword/newword/g' {} \;
Search in this website
our sponsors
latest comments
- sagar on List of all standard version of c language
- Mohit Dhukia on How to access/unblock songs.pk in india?
- shinto peter on How to configure mail from localhost ( wamp ) using PHP?
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language