IPC using FIFO in UNIX environment - inter-process-communicat

I am trying to implement a IPC using FIFO. The code for sender is as follows..
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFER_SIZE 100
#define LISTENER1 "listener1"
char news[BUFFER_SIZE];
void broadcast()
{
int fd1;
mkfifo(LISTENER1,0644);
fd1=open(LISTENER1,O_WRONLY);
printf("\nReady to broadcast\n");
do {
printf("\nEnter message : ");
scanf("%s",news);
write(fd1,news,strlen(news));
} while (1);
return;
}
int main()
{
broadcast();
return 0;
}
The code for receiver is as follows..
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFER_SIZE 100
#define LISTENER "listener1"
char news[BUFFER_SIZE];
void receive()
{
int fd1,num;
mkfifo(LISTENER,0644);
fd1=open(LISTENER,O_RDONLY);
while((num=read(fd1,news,BUFFER_SIZE))>0)
{
news[num]='\0';
printf("\n Received : %s",news);
}
return;
}
int main()
{
receive();
return 0;
}
I run the two programs in two terminal window. The problem i am facing is that the messages are not getting delivered intently. Suppose i type a message in the window of the sender, it does not appear in the receiver's window until and unless i type the next message in the sender's window. I want that the messages be delivered as soon as the return key is pressed. Please help!

Related

Receiver not receiving data sent via segqueue

RECEIVER
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/*
sigval_int
sigval_ptr
*/
void signal_handler(int signum, siginfo_t *siginfo, void *ucontext) {
printf("I got value %d\n", siginfo->si_int);
}
int main() {
pid_t pid = getpid();
struct sigaction act;
printf("My PID is %d\n", pid);
act.sa_sigaction = signal_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO | SA_RESTART;
sigaction(SIGUSR1, &act, NULL);
while (1);// sleep(20);
return EXIT_SUCCESS;
}
SENDER
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
int main(int argc, char *argv[]) {
pid_t pid;
union sigval sv;
if (argc < 2 || (pid = atoi(argv[1])) < 0) exit(EXIT_FAILURE);
sv.sival_int = 56;
printf("sender: sending %d to PID %d\n", sv.sival_int, pid);
sigqueue(pid, SIGUSR1, sv);
return EXIT_SUCCESS;
}
I am trying to run these two processes in two different terminals. First I ran the receiver to note down its PID. Next I used that pid to give command line arguments while executing sender.
The sender shows that it sent the data. But apparently the receiver did not get it. It was as if the signal handler was never invoked. Can someone help with some way to solve this.
TIA

why my code which includes hashfunction and unordered_set uncompilable?

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
struct hashfunction{
size_t operator()(pair<string,int> x){
size_t ret=5381;
for(int i=0;i<x.first.length();i++){
ret=(ret<<5)+(ret)+x.first[i];
}
ret+=x.second;
return ret;
}
};
unordered_set<pair<string,int>,hashfunction> myset;
int main(void)
{
string a="123";
int b= 4;
myset.insert({a,b});
}
I am studying unordered_set and hashfunction. But the code above is not compilable. why not? Anyone can explain why it is not compilable and how to solve this problem?
const is needed after operator()(pair<string,int> x).

I can't fragment my packet on a TCP connection

I've been trying and trying to fragment my TCP packets but I havent found any helpful implementation of it. I am familiar with the theory and concepts of fragmentation, have even come across some flags such as IP_PMTUDISC_DONT, IP_PMTUDISC_WANT, and IP_PMTUDISC_DO and set them but the wireshark's capture always showed DF Flag as on.
I've set the MTU of my 'lo' Network Interface to 1500 since I'm using LoopBack Address on both, the server and the client. And I thought that fragmentation will be handled by the Network Layer, but thats not the case I guess...
Please help me with fragmentation of the packet.Here's my code...
Server.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netdb.h>
int main(){
struct sockaddr_in serv_addr, cli_addr;
char buff[255];
int FileDesc = socket(AF_INET,SOCK_STREAM, 0);
if(FileDesc < 0){
perror("Socket Creation Failed. ");
exit(EXIT_FAILURE);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(5455);
if (bind(FileDesc, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
perror("Bind Failed.");
exit(EXIT_FAILURE);
}
if (listen(FileDesc, 6) < 0){
perror("Listen Failed.");
exit(EXIT_FAILURE);
}
socklen_t cliLen;
int AcceptFD = accept(FileDesc, (sockaddr*)&cli_addr, &cliLen);
if(AcceptFD < 0){
perror("Accept Failed.");
exit(EXIT_FAILURE);
}
//Setting the MTU
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;//address family
strncpy(ifr.ifr_name, "lo", sizeof(ifr.ifr_name));//interface name where you want to set the MTU
ifr.ifr_mtu = 1500; //your MTU size here
if (ioctl(FileDesc, SIOCGIFMTU, (caddr_t)&ifr) < 0){
perror("ioctl error.");
exit(1);
}
std::cout << ifr.ifr_ifru.ifru_mtu;
while(1){
//File Transfer
}
close(AcceptFD);
close(FileDesc);
return 0;
}
Client.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
int main(){
struct sockaddr_in serv_addr;
char buffer[255];
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0){
perror("Socket Creation Failed.");
exit(EXIT_FAILURE);
}
FILE *fp;
char *filename = "File.txt";
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
inet_aton("127.0.0.1", &serv_addr.sin_addr);
serv_addr.sin_port = htons(5455);
int conn = connect(fd, (const sockaddr*)&serv_addr, sizeof(serv_addr));
if (conn < 0){
perror("Connect Failed. ");
exit(EXIT_FAILURE);
}
while(1){
//File Recieve
}
close(fd);
return 0;
}
This also begs the question, how would I re-assemble my packet back in the same order?
Thank youu.

Thrust Device Vector iterator location

i am trying to find the index/location of an iterator, i use the thrust::distance(). however, it returns weird value.
the vector size is 10. and when i use this method it returns value of "131" .
here is a fully working example.
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/random.h>
#include <thrust/unique.h>
#include <thrust/reduce.h>
#include <thrust/iterator/constant_iterator.h>
using namespace std;
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, ""));
std::cout << std::endl;
}
int main()
{
thrust::device_vector<int> x;
x.push_back(1);
x.push_back(10);
x.push_back(1);
x.push_back(11);
x.push_back(1);
x.push_back(11);
thrust::device_vector<int> y(10);
print_vector("Original",x);
thrust::sort(x.begin(),x.end());
print_vector("sort",x);
thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end());
std::cout<<*it<<std::endl;
//int newsize=it-y.begin();
int newsize=thrust::distance(y.begin(),it);
cout<<"nsz:"<<newsize<<endl;
return 0;
}
The iterator it is established with respect to the vector x:
thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end());
^ ^
But you are asking for the distance from this iterator to the beginning of the vector y:
int newsize=thrust::distance(y.begin(),it);
^
That doesn't make sense. There is no defined relationship between it and the vector y.
If you ask for the distance to the beginning of vector x instead, you'll get more sensible results:
$ cat t1244.cu
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/random.h>
#include <thrust/unique.h>
#include <thrust/reduce.h>
#include <thrust/iterator/constant_iterator.h>
using namespace std;
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
}
int main()
{
thrust::device_vector<int> x;
x.push_back(1);
x.push_back(10);
x.push_back(1);
x.push_back(11);
x.push_back(1);
x.push_back(11);
thrust::device_vector<int> y(10);
print_vector("Original",x);
thrust::sort(x.begin(),x.end());
print_vector("sort",x);
thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end());
std::cout<<*it<<std::endl;
//int newsize=it-y.begin();
int newsize=thrust::distance(x.begin(),it);
cout<<"nsz:"<<newsize<<endl;
return 0;
}
$ nvcc -o t1244 t1244.cu
$ ./t1244
Original 1 10 1 11 1 11
sort 1 1 1 10 11 11
10
nsz:3
$

Three threads one for input from keyboard second for encryption and third to print the input

I have to make three threads T1,T2,T3. T1 should take input from keyboard as a string.T2 should encrypt the input. T3 should print the string. So the problem is to serialize threads T1,T2 and T3. Here is my code.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_t inp,encr,prnt;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;;
sem_t sem;
char input_string[30];
void Input(void *ar){
pthread_mutex_lock(&mutex);
fflush(stdin);
scanf("%s",input_string);
pthread_mutex_unlock(&mutex);
}
void Encrypt(void* ar){
pthread_mutex_lock(&mutex);
input_string[0]='a';
input_string[1]='b';
pthread_mutex_unlock(&mutex);
}
void Print(void *ar){
pthread_mutex_lock(&mutex);
printf("%s\n",input_string );
pthread_mutex_unlock(&mutex);
// sleep(1000);
}
int main(){
sem_init(&sem,0,1);
pthread_create( &inp, NULL, Input, (void*)NULL);
pthread_create( &encr, NULL, Encrypt, (void*)NULL);
pthread_create( &prnt, NULL, Print, (void*)NULL);
//while(1){
pthread_join(inp,NULL);
//sleep(3000);
pthread_join(encr,NULL);
//sleep(4000);
pthread_join(prnt,NULL);
// }
return 0;
}